There are two main cases of data transfer between controllers encountered in iOS development: transitive and inverse. Transitive means that the direction of data transmission is the same as the direction of the controller's jump (1), and the inverse transfer means that the direction of data transmission is opposite to the direction of the controller's jump (2). Here are the specific approaches to each of these two delivery methods.
Shun Pass
If the use of SB, Shun transmission is simpler, only in the Prepareforsegue:sender: method of a controller to obtain Destinationviewcontroller according to the Segue parameter, that is, the target controller C, directly to the controller C pass data. The data from a is obtained in the C Viewdidload method, which then uses the data to do some business operations, such as assigning values to UI controls on C.
Reverse Transfer
Reverse transfer or non-SB development need to use proxy mode, simply speaking: Let a become a C agent, in C call a proxy method, through the parameters of the proxy method to pass the data to a. This can be divided into the following four steps:
- 1. Define the protocol in the. h file in C (the Protocol is the Declaration of a bunch of methods, the implementation of which is to follow the protocol A to complete) and add a delegate property to set the proxy object;
- 2. In the M file of C also call the Proxy method to notify its agent;
- 3.A complies with the protocol and implements the proxy method.
Case:
Click "Select" in a to jump to the C controller, select the corresponding data row and return to a and pass the selected data to a for display (e.g.).
A C A
1. Define the protocol in the. h file of controller C and add the delegate attribute:
1 #import<UIKit/UIKit.h>2 3 @classSelectdeptviewcontroller, Department;4 5 @protocolSelectdeptviewcontrollerdelegate <NSObject>6 7 @optional8 9- (void) Selectdeptviewcontroller: (Selectdeptviewcontroller *) SELECTDEPTVC didselectdept: (Department *) department;Ten One A @end - - @interfaceSelectdeptviewcontroller:uitableviewcontroller the -@property (nonatomic, weak)ID<SelectDeptViewControllerDelegate>Delegate; - - @end
2. Call the proxy method in the. m file of Controller C and pass the data through the proxy method to its proxy:
1 #pragmaMark-uitableview Delegate2- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath3 {4 if([Self.Delegaterespondstoselector: @selector (selectdeptviewcontroller:didselectdept:)]) {5Department *dept =Self.departments[indexpath.row];6[Self.Delegateselectdeptviewcontroller:self didselectdept:dept];7 }8 9 //Close the current controllerTen [Self.navigationcontroller Popviewcontrolleranimated:yes]; One}
The 3.A controller implements the agent method of C and gets the data for business operation:
1 # pragma mark- selectdeptviewcontrollerdelegate method 23 -(void) Selectdeptviewcontroller: (Selectdeptviewcontroller *) SELECTDEPTVC didselectdept: (Department *) Department 4 {5 self.department.text = department.name; 6 }
Summarize:
In fact, the agent mode in iOS development is very common. For example, TableView to display data, but it itself is no data, the data in the controller, which involves TableView access to the data in the controller, we use the code or drag line to make the controller TableView agent, and implement the TableView proxy method in the controller.
iOS development: data transfer between controllers