Attribute Value Transfer Between iOS pages, proxy value transfer, and ios page
When the mobile APP is running, it is essential to pass values between different pages. There are many ways to pass values (method-based value transfer, attribute-based value transfer, proxy-based value transfer, and single-case value transfer ), here we will mainly summarize the attribute value passing and proxy value passing.
Attribute Value passing: attribute value passing is the simplest and most common value passing method, but it has limitations (generally used to pass the value of the first page to the second page, but it cannot be uploaded from the second page to the first page ),
Send a value to SecondViewController: SecondViewController sets the attribute sendMessage
1 - (void)rightButtonAction:(UIBarButtonItem *)sender{2 SecondViewController *secondVC = [[SecondViewController alloc]init];3 secondVC.sendMessage = self.rootView.textField.text;4 [self.navigationController pushViewController:secondVC animated:YES];5 }
Value passing by proxy: difficult and difficult to understand. It is usually used to pass a value to the first page on the second page. Generally, it is divided into six steps.
(This example uses navigationController to jump to the page)
1. Declare the Protocol (written on the second page)
@protocol myDelegete <NSObject>- (void)sendMessage:(NSString*)message;@end
2. Define the protocol-compliant attributes (written on the second page) (assign is required for the attributes)
@property (nonatomic , assign)id<myDelegete> delegate;
3. comply with the agreement (written on the first page)
1 @interface RootViewController : UIViewController <myDelegete>
4. Set the proxy (set the proxy to be written in the jump event) (written on the first page)
1-(void) rightButtonAction :( UIBarButtonItem *) sender {2 SecondViewController * secondVC = [[SecondViewController alloc] init]; 3 secondVC. sendMessage = self. rootView. textField. text; 4 [self. navigationController pushViewController: secondVC animated: YES]; 5 // The fourth step 6 secondVC. delegate = self; 7 8}
5. Implementation Protocol method (written on the first page)
1 - (void)sendMessage:(NSString *)message{2 self.rootView.textField.text = message;3 }
6. Implement value transfer (written on the second page)
1-(void) leftButtonAction :( UIBarButtonItem *) sender {2 [self. navigationController popViewControllerAnimated: YES]; 3 // Step 6: 4 [self. delegate sendMessage: self. secondView. textField. text]; 5}