Seven commonly used methods of transmission of values

Source: Internet
Author: User

Simple sharing under, hope that Daniel more guidance maze, gradually revise, perfect insufficient support. For reference, please also understand

1. Single-pass value: <Singleton>

Singleton mode: A class has only one instance and provides a static instance creation method for working with very frequent instances (improved efficiency performance)

First, create a singleton class: @property (Strong, nonatomic) NSString *data; Static Singleton *share=nil;

+ (Singleton *) getinstance{

Static dispatch_once_t once; GCD Technology (Multi-threaded access) executes only once for the entire life cycle

Dispatch_once (&once, ^{share=[[self alloc] init];         });    return share; This class of objects is initialized once

Second, apply to the different view controller: 1, the forward mode form:

-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{

A:singleton *share=[singleton getinstance];   Share.data=xx;     The Singleton class receives the data of a Bviewcontroller *b=segue.destinationviewcontroller//Specifies object B of the target view, and jumps to the instantiation object of B}       Jump after--b:singleton *share=[singleton getinstance]; Xx=share.data;//b receive single-class data 2, Reverse: button monitoring,Receive on-(void) didviewappear{receiving data from a singleton class}

2, notification mechanism value : The Nsnotificationcenter provides a more decoupled approach. The most typical application is any object pair that can send notifications to the center, while any object can listen to the notification of the center.

1, the code to send the notification is as follows:

[[Nsnotificationcenter Defaultcenter] postnotificationname:@ " password identification " Object:dataobj]; & Object:nil Userinfo:dict];
2. The code to receive the notification is as follows:

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (receivedata:) name:@ " Password identification " Object:nil];

3, the registrant can repeatedly call the method to receive data, to achieve the value-transfer function

-(void) Receivedata: (nsnotification *) sender{

xxx = Sender.object; Receive the data passed over:& sender.userInfo:NSDictionary type

You can specify a specific broadcaster object when registering for notifications, but this is not required. You may have noticed the defaultcenter. This is actually the only center you'll use in your app. Notifications are open to the entire application, so there is only one center.

There is also a nsdistributednotificationcenter. This is used for inter-application communication. There is only one center of that type on the entire computer.
Pros: Both the sender and the recipient of the notification do not need to know each other. You can specify the specific method to receive notifications. The notification name can be any string.
Disadvantage: More code is required to observe the key value. You must remove listeners before deleting them. You cannot pass a large number of values, only let who do what.

3. Attribute passing value: forward value. <segue Transfer value >

Stack mode (the first view must have a navigation controller)//b View defined attribute data for receiving data

-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{//a view push, pop

Bviewcontroller *b=segue.destinationviewcontroller;    b.data=xxx; Take B as the destination view, receive data}

button click event to jump to the next view, you need to initialize the second view and act as the target view controller

4, Method transfer value: The value of the same attribute of demand

By using the method to pass the value, you can directly merge the method with the initialization method, when trigger a button click event and jump to B, in the button click event can be directly through the initialization of B, the value is saved in A. First, the B view needs to have an attribute data to storethe value passed over:
1, rewrite the initialization method, used to pass the value: -(ID) Initwithdata: (NSString *) data;
 2. Button Response : Write method values to initialize with:

Bviewcontroller *b = [[Bviewcontroller alloc]initwithdata:data]; At this point, the value is already present in a

[Self.navigationcontroller pushviewcontroller:b Animated:yes];

5, the protocol transfer value instead of the value of the Protocol proxy, the main point in time. <Delegate>

How to enter B from a, and return to a after B input value: Use Delegate (Trust Agreement).
1, A comply with Datadelegate:-(void) onclick: (NSString *) sender{//The method of the present entrustment agreement xx = sender; }//Receive data2, click on the method to enter B

@property (nonatomic,weak) ID <ButtonDelegate>delegate; The object that passes the value is a delegate property. Any object (ID) that implements the delegate agreement

[_delegate Onclick:datab]; //Delegate property invokes the protocol method, passing the data

[Self dismissviewcontrolleranimated:yes completion:nil]; Close Modal &present

6, block transfer value: Reference http://liuyafang.blog.51cto.com/8837978/1551399

1. Declare a block on the first page, need to pass in a color, let the current view color

Declare a block, you need to pass in a color, so that the current view color

void (^changecolor) (uicolor *color) = ^ (Uicolor *color) {

Self.view.backgroundColor = color;

};

2. First page//block value---------will block to the second page

Secondviewcontroller *SECONDVC = [[Secondviewcontroller alloc] init];

Block Pass value---------block to second page

Secondvc.block = ChangeColor;

3. The second page is defined--when the block variable acts as a property of a class, you must use the copy modifier

Block Pass value---------block to second page

Block value---When the block variable is a property of a class, you must use the copy modifier

@property (nonatomic, copy) void (^block) (Uicolor *color);

4. Pass the value to block on the second page

Block value---------will be passed to block

Nsarray *array = [Nsarray arraywithobjects:[uicolor yellowcolor], [Uicolor Cyancolor], [Uicolor GreenColor], [UIColor bro Wncolor], nil];

Self.block ([array objectatindex:rand ()% 4]);

7, data sharing: The app to share data, because each app is sandbox. And sometimes they have to share data with other apps.

    • Uidocumentinteractioncontroller
      Availability:ios 3.2+
      For specific usage see: http://mobile.tutsplus.com/tutorials/iphone/previewing-and-opening-documents-with-uidocumentinteractioncontroller/

    • Uiactivityviewcontroller
      Availability:ios 6.0+

    • Shared Keychain Access
      This requires the same certificate to be used between the app

    • Custom URL Scheme
      Pass the data as a parameter by constructing the URL. Tested locally, pass 10,000 characters can be, but not too long, memory may be unbearable.

    • WEB Service shares data through Dropbox or other third-party services.

    • Uipasteboard + URL scheme passes the name of Uipasteboard through URL scheme and then shares the data through Uipasteboard.
      This is how the IOS SDK should be used.
      On iOS 7, however, there are problems with this approach, and if you do, you have to find a solution.

1) http://enharmonichq.com/sharing-data-locally-between-ios-apps/

2) Http://stackoverflow.com/questions/17080074/ios7-beta-doesnt-allow-inter-app-communication-by-uipasteboard

Seven commonly used methods of transmission of values

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.