Summary of some ways to transfer values between iOS pages

Source: Internet
Author: User


There are 6 ways to say it: 1, attribute value, 2, block;3, Delegate;4, Userdefault;5, Singleton, 6, notice (space reasons I only write the core code, if you can not read directly in the bottom of the original reading to GitHub to see the demo).

1, block (personal feel most commonly used in the scene under the best use)

First say my most commonly used block bar, the property value is very simple, mainly used for Shun Chuan, we are here to include the following are mainly about the inverse of the transmission. Attribute values are written together in the block.

The following code:

SecondVc.h

typedef void (^testblock) (NSString *str);

@interface Atnextviewcontroller:uiviewcontroller

Define Block

@property (nonatomic, copy) Testblock Testblock;

@end

Secondvc.m

-(void) Btnclick: (UIButton *) btn {

if (self.testblock) {//block Pass value

Self.testblock (@ "green");

}

[Self.navigationcontroller Popviewcontrolleranimated:yes];

}

Here is the first VC code:

Vc.m

-(void) Btnclick: (UIButton *) btn {

Atnextviewcontroller *NEXTVC = [[Atnextviewcontroller alloc] init];

Nextvc.instr = @ "Red"; Attribute transfer value, used for passing, direct transmission is good

This is the value of the block callback.

Nextvc.testblock = ^ (NSString *str) {

NSLog (@ "%@", str);

};

[Self.navigationcontroller PUSHVIEWCONTROLLER:NEXTVC Animated:yes];

}

2, Delegate

Agent to first make clear who pass value to who, I mainly write here is the inverse, the inner layer controller to the outer controller, that is SECONDVC value to the VC, so should be VC as SECONDVC Agent, in the proxy method to receive the value of SECONDVC.

SECONDVC's Code:

SecondVc.h

Declaring an agent

@protocol atnextvcdelegate <NSObject>

@optional

-(void) INSTR: (NSString *) inStr;

@end

@interface Atnextviewcontroller:uiviewcontroller

@property (nonatomic, weak) id<atnextvcdelegate> delegate;

@end

Secondvc.m

Proxy pass-through value

-(void) Btnclick: (UIButton *) btn {

if ([Self.delegate respondstoselector: @selector (inStr:)]) {

[Self.delegate instr:@ "Red"];

}

[Self.navigationcontroller Popviewcontrolleranimated:yes];

}

VC Code:

Vc.m

Proxy method Receive value

-(void) Btnclick: (UIButton *) btn {

Atnextviewcontroller *NEXTVC = [[Atnextviewcontroller alloc] init];

Nextvc.delegate = self;

[Self.navigationcontroller PUSHVIEWCONTROLLER:NEXTVC Animated:yes];

}

#pragma mark-atnextvcdelegate

-(void) INSTR: (NSString *) inStr {

NSLog (@ "%@========", inStr);

}

3, Userdefault

This way, the file is stored locally, and is part of the data persistence (about the time the data is persisted in another detail). Opening the program again will still have the value left by the last save.

There are only two actions to save and take:

Secondvc.m

-(void) Btnclick: (UIButton *) btn {

Save

Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];

[Defaults setobject:@ "Yellow" forkey:@ "Btncolor"];

[Defaults setobject:@ "100x100" forkey:@ "btnsize"];

[Defaults synchronize];

[Self.navigationcontroller Popviewcontrolleranimated:yes];

}

Vc.m

-(void) Viewwillappear: (BOOL) Animated {

[Super viewwillappear:animated];

Take

Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];

NSString *btncolor = [Defaults objectforkey:@ "Btncolor"];

NSString *btnsize = [Defaults objectforkey:@ "Btnsize"];

NSLog (@ "%@%@", btncolor,btnsize);

}

In contrast to the previous one, this is a way to enter the interface and it is not necessarily just to return the value from the previous interface.

4. Single Case

Single case generally directly into a macro, use the time to use it directly, so the single macro file here is not written, interested can go to the demo to see, directly on the code:

Here I define a singleton class that uses it to store information about the user's login status, followed by the use of the controller:

ATLoginStatus.h

#import <Foundation/Foundation.h>

#import "ATSingleton.h"

@interface Atloginstatus:nsobject

@property (nonatomic, assign) BOOL IsLogin;

@property (nonatomic, copy) NSString *phonenumstr;

Singletonh (LoginStatus)

@end

Atloginstatus.m

#import "ATLoginStatus.h"

@implementation Atloginstatus

Singletonm (LoginStatus)

@end

Secondvc.m

-(void) Btnclick: (UIButton *) btn {

Save

Atloginstatus *status = [Atloginstatus sharedloginstatus];

Status.phonenumstr = @ "12345";

Status.islogin = YES;

[Self.navigationcontroller Popviewcontrolleranimated:yes];

}

Vc.m

-(void) Viewwillappear: (BOOL) Animated {

[Super viewwillappear:animated];

Take

Atloginstatus *status = [Atloginstatus sharedloginstatus];

NSLog (@ "%@%d", status.phonenumstr,status.islogin);

}

5. Notice

Notice is mainly issued and received notice two:

To send notifications:

Secondvc.m

-(void) Btnclick: (UIButton *) btn {

Send a notification

[[Nsnotificationcenter Defaultcenter] postnotificationname:@ "Btncolornoti" object:self userInfo:@{@ "BtnColor": @ "Yellow "}];

[Self.navigationcontroller Popviewcontrolleranimated:no];

}

Receiving NOTICE:

Vc.m

-(void) Viewdidload {

[Super Viewdidload];

//...

Registration Notice

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (btncolornoti:) name:@ "Btncolornoti" Object:nil];

}

Call this method to accept the notification message when the notification is received

-(void) Btncolornoti: (nsnotification *) Noti {

Nsdictionary *dict = Noti.userinfo;

NSLog (@ "%@", dict[@ "Btncolor"]);

}

Logoff notification monitoring when controller is destroyed

-(void) Dealloc {

[[Nsnotificationcenter Defaultcenter] removeobserver:self];

}

OK, that's all, although these methods can be used for the transfer of values between controllers, but in fact there are some ways it is not necessary to use in my example of this common scenario (run my demo to understand this is what the scene) under (Of course, the property value can only be used for the pass), in my example of this scenario, With block is the most concise, delegate can also, but personal feeling for the value of too much trouble, notice also can, but still not as block. However, Singleton and userdefaults do not need to be used in this scenario, they can be used in the case where two controllers are very far away, or if two controllers are not associated with each other. As in the case of my singleton example, it is possible to save all the information that can be used globally, so that the information can be obtained at all times.

Below is demo, click to read the original view.

Summary of some ways to transfer values between iOS pages

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.