IOS Page Pass Value method
Apply to:
- Two Interactive interface : 1) Page one jumps to page two, the TextField value of page one is passed to the label of Page two. 2) a page jump to b page, b page and then jump back to a page (registration page and login page)
- two non-interactive two interfaces : The data persistence layer interacts with the presentation layer's data.
Several ways of transmitting value:
- Property Pass -through value
- Delegate Delegate Way
- Notice Notification Way
- Block Way
- Singleton Mode method
- Userdefault or File Mode
1. attribute value (pass-through):
- You need to define an exposed property
- Need an object that you need to pass a value to
- Assigning values to Properties
Implementation: A, B two interface, through the button to the A interface TextField value to page B label.
A:viewcontroller B:detailviewcontroller
#import "ViewController.h" @interfaceViewcontroller () @property (nonatomic, strong) Uitextfield*TextField;@end @implementationViewcontroller-(void) Buttonaction: (UIButton *) Button{detailviewcontroller*detailviewcontroller =[[Detailviewcontroller alloc]init];//use the properties of the next view to accept the values that you want to pass through, the property pass valueDetailviewcontroller.firstvalue=Self.txtFiled.text; [Self.navigationcontroller Pushviewcontroller:second animated:yes];} Detailviewcontroller Interface#import "DetailViewController.h" @interfaceDetailviewcontroller () @property (nonatomic, weak) nsstring firstvalue;//receives the value that the TextField passes over. @end
2. Proxy value (can be passed backwards)
Delegate can only be one-to-one. He can only be the navigation of the stack inside the adjacent controller value, can not pass the value across the controller. For example: A to B,b to C, and not from C to a.
- Draft agreement (usually named Xxxdelegate), the parameters of the law in the agreement depend on what you want to deliver.
- Set agent properties (again, use assign anti-loop reference)
- In the case of the need to adjust the adjustment, this step is more abstract, as described above, we need to click on the button
- Pass the value and cancel the interface, so our delegate is on the inside of this? call
- Let the corresponding object become the agent, is it like to specify a proxy at the time of creation?
- Comply with the Protocol, implement the appropriate method, and then do the logical processing in the method
Implementation: A page push to B page, if the information of B page wants to return (callback) to a page, with the proxy value, where B defines the protocol and the declaration agent, a to confirm and implement the agent, a as a proxy for B
Page A: rootviewcontrollers b page: detailviewcontrollers
ViewControllers.h File#import<UIKit/Uikit.h>@classDetailviewcontroller;@protocolPassingvaluedelegate<nsobject>//Draft Agreement@optional-(void) Passvalues: (NSString *) values;//define the protocol method (values you want to pass in the content)@end@interfaceViewcontrollers:uiviewcontroller@property (nonatomic, weak)ID< Passingvaluedelegate >Delegate; (valuedelegate)//set proxy properties, passing values (weak or assign are used here to prevent circular references)Click the button event function (Invoke proxy)-(void) trendbtnclick{//Create the ViewDetailviewcontroller* Detailviewcontroller = [[Detailviewcontroller alloc] Initwithnibname:@"Detailviewcontroller"Bundle:nil];//Pass the value and cancel the interfaceSelf .Delegate(self-named valuedelegate) = Detailviewcontroller;//set up the agent to make the corresponding object a delegate[self.trenddelegate passvalues:@"123456"]; //page Jumpthe}detailviewcontroller.h file references the Viewcontroller header file and adds the proxy protocol as follows#import "ViewController.h" @interfaceTrendviewcontroller:uiviewcontroller<passtrendvaluedelegate>{ } @endTo implement the proxy function:#pragmaMark implements the method of value-Transfer Protocol-(void) Passvalues: (NSString *) values{NSLog (@"values=%@", values); }
The most important is also the most easy to ignore, is to set the point of delegate
3. Notification pass value (can be passed backwards) the value of the third page is passed to the first page
Who wants to monitor the value of the change, who will register notice, in particular, notice that the recipient of the notification must exist this prerequisite without notice, remember to remove.
- In the third interface, build a notification hub, send notifications via notification hubs (the process of sending notifications is the process of passing values, passing the value of the transfer as the value of the object to the first interface
- In the first interface, a notification center, through the notification, register a listener event
- In the first sector, set up the event that receives the notification.
- In the dealloc of the first sector, remove the notification centre
Any object pair 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:
[[Nsnotificationcenter Defaultcenter] postnotificationname:@ "Mynotificationname" object:broadcasterobject];
The code to register to receive notifications is as follows:
[[Nsnotificationcenter Defaultcenter] addobserver:listenerobject selector: @selector (Receivingmethodonlistener :) name:@ "Mynotificationname" object:nil];
Implementation: The value of the B interface is passed to the a interface, so the B interface sends notifications to the Notification center, a interface registers as a listener, listens for changes in B values, and accepts the value of B returned from the Notification center. If the C page also wants to accept the value of B, it is also registered as a listener. (So many-to-many can be achieved)
A interface: Rootviewcontroller B Interface: Secondviewcontroller
in the B interface to establish notification center, the value to be passed Nametextfield.text send via notification hubs
//SECONDVIEWCONTROLLER.M-(Ibaction) NotificationMethod: (IDsender {[[Nsnotificationcenter Defaultcenter] postnotificationname:@'changenamenotification' Object: Self userinfo:@{@'name': Self.nameTextField.text}]; [Self Dismissviewcontrolleranimated:yes completion:nil];} In the controller on page A, register the notification://ROOTVIEWCONTROLLER.M- (void) viewdidload. {[Super Viewdidload]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (changenamenotification:) name:@'changenamenotification' Object: nil];} Called, displays//ROOTVIEWCONTROLLER.M-(void) Changenamenotification: (nsnotification*) Notification{nsdictionary*namedictionary =[Notification Userinfo];self.namelabel.text= [Namedictionary objectforkey:@'name'];.} When we do not use, remember to delete the notice://ROOTVIEWCONTROLLER.M-(void) Dealloc{[[nsnotificationcenter Defaultcenter] removeobserver:self];}
4. Block transfer value (can be passed backwards) block is also used for callbacks
- Alias The block. and parameter write parameters that need to be passed in the parameter list
- Set block properties (note using copy)
- Set a method to hold the current block
- Call a similar agent in the right place
- Make block calls where the object is created
Block syntax
return value type (^block parameter name) = ^ return value type (argument type parameter name)
Implementation: Pass the textfield.text of the B interface to the label of the A interface
Page A: rootviewcontrollers b page: detailviewcontrollers
Detailviewcontroller file
#import<UIKit/Uikit.h>typedefvoid(^detailblock) (NSString *);//The block takes an alias. and parameter write parameters that need to be passed in the parameter list@interfaceDetailviewcontroller:uiviewcontroller@property (nonatomic, copy) Passingvalueblock Passingvalue;//Set block Properties (note using copy)@property (Weak, nonatomic) Uitextfield*Inputtf;@end-(Ibaction) Btnaction: (ID) Sender {//determine if block is empty if(self.) Nextviewcontrollerblock) {self. Nextviewcontrollerblock (Self.inputTF.text); } [Self.navigationcontroller Popviewcontrolleranimated:yes];}//Click the button to the A interfaceRootviewcontroller.m@property (Strong, nonatomic) UILabel*Textlabel;-(void) Handlebutton: (nsstring*) Sender{detailviewcontroller*detailviewcontroller =[[Detailviewcontroller alloc]init];d etailviewcontroller.passingvalue=^ (nsstring*str) {self. Textlabel.text=str;} [Self.navigationcontroller Pushviewcontroller:detailviewcontroller animated:yes];}
5. Single-pass value (all can be passed down)
The advantage of a singleton is that it is only created once, the rest of the time access to the object is the same, so many times the user's information is stored in a single case, so that it does not need to pass the value, only need to create a single example to be able to
AppStatus.h Create a singleton class Appstatus
#import <Foundation/Foundation.h>
@interface Appstatus:nsobject
{
NSString *_contextstr;
}
@property (Nonatomic,retain) NSString *contextstr;
+ (Appstatus *) shareinstance;
@end
Appstatus.m
#import "AppStatus.h"
@implementation Appstatus
@synthesize contextstr = _contextstr;
static Appstatus *_instance = nil;
+ (Appstatus *) shareinstance
{
if (_instance = = nil)
{
_instance = [[Super Alloc]init];
}
return _instance;
}
-(ID) init
{
if (self = [super init])
{
}
return self;
}
@end
RootViewController.h
#import "RootViewController.h"
#import "DetailViewController.h"
#import "AppStatus.h"
@interface Rootviewcontroller ()
@end
@implementation Rootviewcontroller
-(void) Pushaction: (ID) sender
{
TF = (Uitextfield *) [Self.view viewwithtag:1000];
Single-pass value will be transferred to the information in a single case (shared)
[[Appstatus Shareinstance]setcontextstr:tf.text]; This is equivalent to the following notation.
[Appstatus shareinstance].contextstr = Tf.text;
Navigating the push to the next page
Detailviewcontroller *detailviewcontroller = [[Detailviewcontroller alloc]init];
Navigating the push to the next page
[Self.navigationcontroller Pushviewcontroller:detailviewcontroller Animated:yes];
}
@end
DetailViewController.h
#import <UIKit/UIKit.h>
@protocol changedelegate;//Notifies the compiler that it has this agent
@interface Detailviewcontroller:uiviewcontroller
{
Uitextfield *textfield;
}
@end
#import "DetailViewController.h"
#import "AppStatus.h"
@interface Detailviewcontroller ()
@end
@implementation Detailviewcontroller
@synthesize navititle = _navititle;
-(void) Loadview
{
Self.view = [[[UIView alloc]initwithframe:cgrectmake (0, 0, +, 480)]autorelease];
Single case
Self.title = [Appstatus shareinstance].contextstr;
TextField = [[Uitextfield alloc]initwithframe:cgrectmake (100, 100, 150, 30)];
Textfield.borderstyle = Uitextborderstyleline;
[Self.view Addsubview:textfield];
Uibarbuttonitem *doneitem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemdonetarget: Self action: @selector (doneaction:)];
Self.navigationItem.rightBarButtonItem = Doneitem;
[Doneitem release];
}
This method is performed multiple times equivalent to refreshing the view
-(void) Viewwillappear: (BOOL) animated
{
[Super viewwillappear:animated];
TF = (Uitextfield *) [Self.view viewwithtag:1000];
Tf.text = [Appstatus shareinstance].contextstr;
}
Pop back to a previous page
-(void) Doneaction: (ID) sender
{
Single-case pass-through value
[Appstatus shareinstance].contextstr = Textfield.text;
[Self.navigationcontroller Poptorootviewcontrolleranimated:yes];
}
6.NSUserDefault Transmit value (pass-pass and Rewind)
[[Nsuserdefault Standarduserdefaults]setobject: value to be passed Forkey:];//setting value for this tag-worthy key
[[Nsuserdefault Standarduserdefaults]objectforkey:];//Get the value according to your tag key
Six ways to pass the value of iOS learning