Briefly describe the seven methods for passing values between UIViewControl ~~~, Seven types of uiviewcontrol

Source: Internet
Author: User

Briefly describe the seven methods for passing values between UIViewControl ~~~, Seven types of uiviewcontrol

I will summarize several methods of passing values between the UIViewControl I learned here. I hope that my shoes will support more ~~~

I. Forward value passing Method

This method should be the easiest way to pass values. We will first create two view controllers, which are currently called OneViewControl and TwoViewControl. Then there is a UIButton (button) on the first view controller) and a UIlabel (Label), the second controller has a UIButton and a UITexField (text box ). Then we add the following code to AppDelegate:

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {

Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds];

Self. window. backgroundColor = [UIColor whiteColor];

[Self. window makeKeyAndVisible];

OneViewController * root = [[OneViewController alloc] init];

Self. window. rootViewController = root;

Return YES;

}

 

In layman's terms, the above Code is the code in OneViewControl, which is very simple when the program is run.

The purpose is to upload the text above this button to the TwoViewControl button when you click the button in OneViewControl.

We declare an attribute in TwoViewControl. h file.

 

@property(nonatomic,copy)NSString *str;

 

Add the following code to the button event in OneViewControl:

 

 

-(Void) onClick :( UIButton *) sender {TwoViewController * twoView = [[TwoViewController alloc] init]; // use the attribute to pass the value twoView. str = sender. titleLabel. text; // jump to the next view and see if there is any animation. For simplicity, no animation is written. [self presentViewController: twoView animated: YES completion: nil];}

 

Okay ~ In this way, the value above the button in TwoViewControl is the same as that in OneViewControl. The value transfer effect is achieved ~~~ It's easy.

 

2. Use a proxy to pass the value (reverse value transfer)

This time, we enter some content in the text box above TwoViewControl and click the button to return to OneViewControl and display the content to the UILabel of OneViewControl. (The so-called reverse value transfer is to transfer the value from the last view controller to the previous View Controller)

First declare the protocol in TwoViewControl. h file: And declare weak reference pointer

@ Protocol TwoViewControllerDelegate <NSObject> // declare the protocol // call-(void) inputString :( NSString *) textStr; @ end @ interface TwoViewController: UIViewController // The delegate declares the weak reference pointer @ property (nonatomic, weak) id <TwoViewControllerDelegate> delegate; @ end

Add the following code to the button event in the. m file of TwoViewControl:

-(Void) onClick {// find the textField text UITextField * tf = (id) [self. view viewWithTag: 2]; [tf resignFirstResponder]; // return data [self. delegate inputString: tf. text]; [self dismissViewControllerAnimated: YES completion: nil];}

Add the following code to the button method in OneViewControl:

-(void)onClick:(UIButton*)sender{    TwoViewController *two = [[TwoViewController alloc]init];    two.delegate = self;        [self presentViewController:two animated:YES completion:nil];}

Okay, this is what the second type of proxy transfers ,~~~ Do not forget to add the Compliance Protocol to the. m file in the first view controller.

3. Notification transfer value (reverse transfer value)

Create a notification object in TwoViewControl and send a notification. Add the following code to the button event:

// The first parameter is the notification name. You must enter the // object sent by the second parameter. // The third parameter is a dictionary with information, nil NSNotification * noti = [NSNotification notifnotifwithname: @ "myNotification" object: self userInfo :@{@ "inputstr": tf. text}]; // send a notification [[nsnotifcenter center defacenter center] postNotification: noti];

[Self dismissViewControllerAnimated: YES completion: nil];

Add the method for listening to notifications and response methods to OneViewControl

// 3. listener notification-(void) viewWillDisappear :( BOOL) animated {[super viewWillDisappear: animated]; nsicationicationcenter * center = [NSNotificationCenter defacenter center]; // The first parameter is who the observer is. // The second parameter is the method called. // The third parameter is the name of the listener. // The object sent by the notification, nil indicates any object [center addObserver: self selector: @ selector (incluenoti :) name: @ "myNotification" object: nil];} // 4. response-(void) incluenoti :( NSNotification *) noti {UILabel * label = (id) [self. view viewWithTag: 1]; label. text = noti. userInfo [@ "inputstr"];}

It should be noted that the name of the notification must be the same on both sides. Otherwise, the notification cannot be received.

4. Use Block to pass values (reverse value transfer)

If you use Block to pass a value, first declare that there is no return value. The Block attribute of a parameter is in TwoViewControl.

@property (nonatomic,copy)void(^returnStrBlock)(NSString*);

In TwoViewControl, add the following code to the button event. The current view calls the block

    UITextField *tf = (id)[self.view viewWithTag:2];    [tf resignFirstResponder];        self.returnStrBlock(tf.text);    [self dismissViewControllerAnimated:YES completion:nil];

In oneViewControl, click the button event to set the callback block function.

TwoViewController * two = [[TwoViewController alloc] init]; // sets the callback block Function two. returnStrBlock = ^ (NSString * inputStr) {UILabel * label = (id) [self. view viewWithTag: 1]; label. text = inputStr ;}; [self presentViewController: two animated: YES completion: nil];

This write is relatively simple, but the effect can be achieved ~~~

5. Use global variables to pass values (use global variables to pass values)

This method is quite low and simple. You can add the following two codes to TwoViewControl and oneViewControl.

NSString *inputStr;
// Reference the data declared in other files extern NSString * inputStr;

I personally do not recommend this method ~~~

6. Pass values in a single example

This method is easy to understand. Add the following code to create a class file.

@ Interface SingletonModel: NSObject @ property (nonatomic, strong) NSString * textStr; // declare SingletonModel + (SingletonModel *) shareSingletonModel; @ end

Implement this class method in the. m file (the singleton mode is mostly used to create instances in this way, which is simple and easy to understand. It's just one thing ~~~)

static SingletonModel *shareObj = nil;@implementation SingletonModel+(SingletonModel *)shareSingletonModel{    if(shareObj==nil)    {        shareObj = [[SingletonModel alloc]init];    }    return shareObj;}@end

Then it's quite easy. In TwoViewControl, pass the value to the singleton object and retrieve this value in OneViewControl ~~~

6. Use AppDelegate to pass the value

To put it simply, declare an attribute in AppDelegate and create an AppDelegate object in TwoViewControl.

 

    AppDelegate *appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;        appdelegate.textStr = tf.text;

 

In OneViewControl

-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    AppDelegate *appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;        UILabel *label = (id)[self.view viewWithTag:1];    label.text = appdelegate.textStr;}

This method is similar to the preceding Singleton mode. To give a simple example, two people need to exchange things, first hand over things to the third person, and then hand over the third person to the other two, in the preceding singleton and AppDelegate modes, the values are equivalent to the third person.

 

Okay ~~~~ Seven methods have finally been written. From the first word to the last symbol, the Code is also typed one by one ~~~ I hope you will understand more about it. If you have any questions, I hope you can talk more about it. If you find it useful, I would like to give you a little thumbs up ~~~ PS: this person is easy to satisfy ~~~ Thank you!

 

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.