Summary of the value of the transfer between IOS pages
1. Attribute Passing value
( 1 ) : attribute value The first step requires what type to define what kind of property
( 2 ) : from the previous page to a page of the selected method will pass the value of the arrival (previous page) Note: This method only applies to the previous page pushed to the next page .
such as: Mainviewcontroller and Secondviewcontroller two view controller , click Mainviewcontroller in the button will jump to Secondviewcontroller view , and you want to pass a value in the past. You can then use the property to pass the value.
First, you need a property in the Secondviewcontroller view to store the values passed in:
@property (nonatomic,retain) nsstring *firstvalue;//Property pass value
Then the Mainviewcontroller view needs to refer to the header file of the Secondviewcontroller view, in the button click event in the View, Objects passed by Secondviewcontroller will need to be passed in a value that exists in Firstvalue:
(void) Buttonaction: (UIButton *) button
{
Secondviewcontroller *second =
[[Secondviewcontroller alloc]init];//uses the next view's properties to accept the value that you want to pass, the value of the property
Second.firstvalue = _txtfiled.text;
[Self.navigationControllerpushViewController:second Animated:yes];
}
After the page jumps, you can take the value you just passed in the Secondviewcontroller view, using the stored Value property:
Displays the value passed in [_txtfiledsettext:_firstvalue];//firstvalue Save the value passed over
2. Notification value
Notification Center
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.
The code to send the notification is as follows:
[[Nsnotificationcenterdefaultcenter] postnotificationname:@ "Mynotificationname" object:broadcasterobject];
The code to register to receive notifications is as follows:
[[Nsnotificationcenterdefaultcenter] Addobserver:listenerobject selector: @selector (Receivingmethodonlistener :) name:@ "Mynotificationname" object:nil];
You can specify a specific broadcaster object when registering for a notification, but this is not required. You might have noticed.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 aNsdistributednotificationcenter. This is used for inter-application communication. There is only one center of that type on the entire computer.
Advantages: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.
Disadvantages:more code is required to observe the key values. You must remove listeners before deleting them. You cannot pass a large number of values, only let who do what.
3, Proxy Protocol transfer value
(1), agreement (Protocol), that is, after the use of this agreement, must be in accordance with the provisions of the Agreement to deal with the matter, the method required by the agreement must be implemented (except @optional method).
Protocol is a syntax that provides a handy opportunity to implement delegate patterns.
The definition protocol is as follows:
1. @protocol classbdelegate<nsobject>
2.-(void) MethodOne;
3. @optional
4.-(void) Methodtwo: (NSString *) value;
5. @end
Defines a CLASSB protocol that contains two methods, where methodtwo is optional.
Implement this protocol in ClassA's header file (ClassA.h), as in the following code:
1. @interface classa<classbdelegate>
2. @end
In the ClassA implementation file (CLASSA.M) Implementation of the Classbdelegate two methods, where methodtwo can not be implemented, as follows:
1.-(void) methodone{
2.//Specific implementation content
3.}
4.
5.-(void) Methodtwo: (NSString *) value{
6.//Specific implementation content
7.}
(2), Agent (delegate), as the name implies is entrusted to other people to do things, when a thing happened, they do not handle, let others to deal with.
Delegate and protocol are not related. Delegate itself is a design pattern. is to take one part of a class that you need to do, so that another class (or itself) can do it.
Define a proxy in the ClassB header file (ClassB.h) as follows:
1. @interface ClassB
2. @property (nonatomic, unsafe_unretained) id<classbdelegate> delegate;
That way, when we encounter problems in the ClassB implementation file (CLASSB.M) that you want other classes (such as ClassA) to handle, you can
1. [Self.delegate MethodOne];
2. [Self.delegate methodtwo:@ "value to be passed"];
Summary of the value of the transfer between IOS pages