IOS-pass value (attribute, proxy (delegate), code block, Singleton, and notification) between pages, and ios-page
(1) attribute value passing
(2) pass value by proxy (delegate)
Pass value by proxy
Applicable
Reverse value transfer (from back to front)
1.1 create a protocol and a protocol method on the SecondViewController page
1.2 create a property of the protocol type. Create the property id <postValueDelegate> delegate in SecondViewController.
1.3 The call attribute is delegate.
On the SecondViewController page, call
[Self. delegate postValue: self. textName. text];
1.4 on the first page, the page showing the modified information is displayed.
Follow the Protocol Implementation Protocol method to specify the proxy object (that is, assign a value to the proxy in the method of passing value parameters on the page)
SecondVc. delegate = self;
File directory:
FirstViewController. h
#import <UIKit/UIKit.h>#import "SecondViewController.h"@interface FirstViewController : UIViewController<UITextFieldDelegate,postValue>@property(strong,nonatomic) UITextField *textName;@property(strong,nonatomic) UIButton *nextbutton;@end
FirstViewController. m
# Import "FirstViewController. h "@ interface FirstViewController () @ end @ implementation FirstViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor redColor]; self. textName = [[UITextField alloc] initWithFrame: CGRectMake (100,100,200, 50)]; self. textName. borderStyle = UITextBorderStyleLine; self. textName. delegate = self; [self. view addSubview: self. textName]; self. nextbutton = [[UIButton alloc] initWithFrame: CGRectMake (200,200,100, 50)]; [self. nextbutton setTitle: @ "next page" forState: UIControlStateNormal]; self. nextbutton. backgroundColor = [UIColor grayColor]; [self. nextbutton addTarget: self action: @ selector (nextPage) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: self. nextbutton];}-(BOOL) textFieldShouldReturn :( UITextField *) textField {if ([textField isFirstResponder]) {[textField resignFirstResponder];} return YES;}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {[self. textName resignFirstResponder];}-(void) nextPage {SecondViewController * secondVc = [[SecondViewController alloc] init]; secondVc. str = self. textName. text; secondVc. deletage = self; [self presentViewController: secondVc animated: YES completion: nil];}-(void) postValue :( NSString *) str {self. textName. text = str;}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
SecondViewController. h
#import <UIKit/UIKit.h>
// Create a protocol
@protocol postValue <NSObject>-(void)postValue:(NSString *)str;@end@interface SecondViewController : UIViewController<UITextFieldDelegate>@property(strong,nonatomic) UITextField *textInfo;@property(strong,nonatomic) UIButton *backbutton;@property(strong,nonatomic) NSString *str;@property(strong,nonatomic) id<postValue> deletage;@end
SecondViewController. m
# Import "SecondViewController. h "@ interface SecondViewController () @ end @ implementation SecondViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor colorWithRed: 0.541 green: 0.878 blue: 0.831 alpha: 1.000]; self. textInfo = [[UITextField alloc] initWithFrame: CGRectMake (100,100,200, 50)]; self. textInfo. borderStyle = UITextBorderStyleLine; self. textInfo. text = self. str; se Lf. textInfo. delegate = self; [self. view addSubview: self. textInfo]; self. backbutton = [[UIButton alloc] initWithFrame: CGRectMake (100,200,100, 50)]; [self. backbutton setTitle: @ "Previous Page" forState: UIControlStateNormal]; self. textInfo. text = self. str; [self. backbutton addTarget: self action: @ selector (backPage) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: self. backbutton];}-(BOOL) textFie LdShouldReturn :( UITextField *) textField {if ([textField isFirstResponder]) {[textField resignFirstResponder];} return YES;}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {[self. textInfo resignFirstResponder];}-(void) backPage {if (self. deletage! = 0) {[self. deletage postValue: self. textInfo. text];} [self dismissViewControllerAnimated: YES completion: nil];}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
AppDelegate. h
#import <UIKit/UIKit.h>#import"FirstViewController.h"@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end
AppDelegate. m
#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { FirstViewController *firstVc=[[FirstViewController alloc] init]; self.window.rootViewController=firstVc; return YES;}
.......
@ End
(3) passing values in code blocks
(4) Notification transfer value
(5) Single-case data transfer