The problem of translating from StackOverflow
Forward Passing Data:
For example, you have Viewcontrollera and Viewcontrollerb, passing a bool variable from Viewcontrollera to Viewcontrollerb:
(1) Create an attribute in ViewControllerB.h
@property (nonatomic, assign) BOOL *issomethingenabled;
(2) Introduction of header file in Viewcontrollera
#import "ViewControllerB.h"
When you click Didselectrowatindex or ibaction, you can set the properties of the Viewcontrollerb with the following code
Viewcontrollerb *viewcontrollerb = [[Viewcontrollerb alloc] initwithnib:@ "Viewcontrollerb" bundle:nil]; viewcontrollerb.issomethingenabled = YES; [Self pushviewcontroller:viewcontrollerb animated:yes];
Passing data in front of the segue:
Pass the data in the following ways:
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender
(1) Create an attribute in ViewControllerB.h
@property (nonatomic, assign) BOOL *issomethingenabled;
(2) Introduction of header file in Viewcontrollera
#import "ViewControllerB.h"
(3) The establishment of a segue from Viewcontrollera to Viewcontrollerb, may be named Showdetailsegue
(4) Implementation method:
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{ if ([Segue.identifier isequaltostring:@] Showdetailsegue "]) { Viewcontrollerb *controller = (Viewcontrollerb *) Segue.destinationviewcontroller; controller.issomethingenabled = YES;} }
If your view is nested inside the navigation controller,
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{ if ([Segue.identifier isequaltostring:@] Showdetailsegue "]) { Uinavigationcontroller *navcontroller = (Uinavigationcontroller *) Segue.destinationviewcontroller; Viewcontrollerb *controller = (Viewcontrollerb *) Navcontroller.topviewcontroller; controller.issomethingenabled = YES;} }
To pass data back:
Viewcontrollera inside click some buttons, jump to Viewcontrollerb, the inside of the event after the completion of the execution, but also jump back to Viewcontrollera, and some data to the Viewcontrollera. Proxy mode is required here.
(1) in ViewControllerB.h, #import下, declare an agreement on the @interface
@protocol viewcontrollerbdelegate <nsobject>-(void) Additemviewcontroller: (Viewcontrollerb *) controller Didfinishenteringitem: (NSString *) item; @end
(2) Setting a delegate property in ViewControllerB.h
@property (nonatomic, weak) ID <ViewControllerBDelegate> delegate;
(3) In Viewcontrollerb, when we quit this viewcontroller,
NSString *itemtopassback = @ "Pass This value, back to Viewcontrollera"; [Self.delegate additemviewcontroller:self Didfinishenteringitem:itemtopassback];
(4) in ViewControllerA.h, introduce the header file and implement the Protocol
#import "ViewControllerB.h" @interface Viewcontrollera:uiviewcontroller <ViewControllerBDelegate>
(5) in VIEWCONTROLLERA.M,
-(void) Additemviewcontroller: (Viewcontrollerb *) controller didfinishenteringitem: (NSString *) item{ NSLog (@ " This is returned from Viewcontrollerb%@ ", item);}
(6) Set up Agent
Viewcontrollerb *viewcontrollerb = [[Viewcontrollerb alloc] initwithnib:@ "Viewcontrollerb" bundle:nil]; Viewcontrollerb.delegate = self[[self Navigationcontroller] Pushviewcontroller:viewcontrollerb Animated:YES];
Transferring data between IOS Viewcontroller