How to transfer values between iOS pages (nsuserdefault/delegate/nsnotification/block/singleton)
The following iOS page values are implemented: 1. Entrustment delegate mode, 2. Notification notification mode, 3.block mode, 4.UserDefault or file mode, 5. Singleton mode; 6. Pass the value between pages by setting properties
In the development of iOS, we often encounter the problem of jumping between pages, and now summarize:
Scenario 1: A page jumps to page b
Method:
In the controller of the B page, write the corresponding property, in the a page to jump to the B page, the B property assignment can be
SecondViewController.h
@property (nonatomic) Nsinteger flag;//current system designation (0: other means of transmission; 1:block pass-through value)
In the attempt controller on page a
Rootviewcontroller.m
-(Ibaction) Showsecondview: (ID) Sender { Secondviewcontroller *second = [[Secondviewcontroller alloc] initwithnibname:@ "Secondviewcontroller" bundle:nil]; Second.delegate = self; Second.flag = 0; [Self Presentviewcontroller:second animated:yes completion:nil];}
Scenario 2: A page jumps to page B, page b jumps back to page a
Mainstream programmes:
(1) through the way of entrusting delegate to achieve
Set up protocols and methods
SecondViewController.h
@interface Secondviewcontroller:uiviewcontroller@property (nonatomic, weak) id<secondviewdelegate> delegate;@ Property (nonatomic, copy) Ablock block; @end
Call
Show
Rootviewcontroller.m
-(void) ShowName: (NSString *) namestring{ self.nameLabel.text = namestring;}
The most important is also the most easy to ignore, is to set the point of delegate.
(2) by notifying the notification of the way to achieve
In the Controller on page b, send the notification:
secondviewcontroller.m-(ibaction) NotificationMethod: (ID) Sender { if ([self notempty]) { [[ Nsnotificationcenter Defaultcenter] postnotificationname:@ "changenamenotification" object:self userInfo:@{@ "name": Self.nameTextField.text}]; [Self Dismissviewcontrolleranimated:yes completion:nil]; } else{ [self showalert];} }
In the controller on page A, register the notification:
rootviewcontroller.m-(void) viewdidload{ [Super Viewdidload]; Do any additional setup after loading the view from its nib. [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (changenamenotification:) name:@ " Changenamenotification "Object:nil";}
When we do not use, remember to delete the notice:
rootviewcontroller.m-(void) dealloc{ [[Nsnotificationcenter Defaultcenter] removeobserver:self];}
Called, displays
rootviewcontroller.m-(void) Changenamenotification: (nsnotification*) notification{ nsdictionary * Namedictionary = [notification userInfo]; Self.nameLabel.text = [namedictionary objectforkey:@ "name"];}
(3) Block mode realization
Block Description: http://blog.csdn.net/totogo2010/article/details/7839061
Link an article describing the block callback very interesting articles: http://blog.csdn.net/mobanchengshuang/article/details/11751671
Analysis:
In the B attempt controller, define a block with the argument as a string
secondviewcontroller.htypedef void (^ablock) (NSString *str);
Secondviewcontroller.h@property (nonatomic, copy) Ablock block;
In the B attempt controller, when the name is entered, click on the corresponding OK button
-(Ibaction) Blockmethod: (ID) Sender { if ([self notempty]) { if (self.block) { Self.block ( Self.nameTextField.text); [Self Dismissviewcontrolleranimated:yes completion:nil]; } } else{ [self showalert];} }
In a try to show that callback block
-(Ibaction) Showsecondwithblock: (ID) Sender { Secondviewcontroller *second = [[Secondviewcontroller alloc] initwithnibname:@ "Secondviewcontroller" bundle:nil]; [Self Presentviewcontroller:second animated:yes completion:nil]; Second.block = ^ (NSString *str) { self.nameLabel.text = str; };}
In the process of accessing the information, I also saw the following scenarios:
(1) Using sharedapplication, define a variable to pass (the same way as the singleton)
(2) using a file, or Nsuserdefault, to pass
The value is stored in a file or Userdefault mode (it is not very suitable for this kind of value, if you want to save the value in file or Userdefault mode, you can consider this way)-(ibaction) Userdefaultmethod: (ID) Sender { if ([self notempty]) { [[nsuserdefaults standarduserdefaults] SetObject:self.nameTextField.text forkey:@ " Mynametext "]; [Self Dismissviewcontrolleranimated:yes completion:nil]; } else{ [self showalert];} }
In a try to display the controller
-(void) Viewdidappear: (BOOL) animated{ [Super viewdidappear:animated]; If you want to test to pass a value by Userdefault method or pass a value in a single case, cancel the following comment */* if ([[[[ nsuserdefaults standarduserdefaults] objectforkey:@ " Mynametext "] length]! = 0) { self.nameLabel.text = [[Nsuserdefaults standarduserdefaults] objectforkey:@" Mynametext "]; [[Nsuserdefaults Standarduserdefaults] setobject:@ "" forkey:@ "Mynametext"]; } DataSource *datasource = [DataSource shareddatasource]; if ([datasource.myname length]! = 0) { self.nameLabel.text = datasource.myname; Datasource.myname = @ ""; } */}
(3) passing through a single case class
b trying to control
Pass a value in a single case (the feeling is not quite suitable for such a pass value, if you want to use a single case to pass the value, you can consider this way)-(ibaction) Singletonmethod: (ID) Sender { if ([self notempty]) { DataSource *datasource = [DataSource shareddatasource]; Datasource.myname = Self.nameTextField.text; [Self Dismissviewcontrolleranimated:yes completion:nil]; } else{ [self showalert];} }
A attempts to display the controller
-(void) Viewdidappear: (BOOL) animated{ [Super viewdidappear:animated]; If you want to test to pass a value by Userdefault method or pass a value in a single case, cancel the following comment */* if ([[[[ nsuserdefaults standarduserdefaults] objectforkey:@ " Mynametext "] length]! = 0) { self.nameLabel.text = [[Nsuserdefaults standarduserdefaults] objectforkey:@" Mynametext "]; [[Nsuserdefaults Standarduserdefaults] setobject:@ "" forkey:@ "Mynametext"]; } DataSource *datasource = [DataSource shareddatasource]; if ([datasource.myname length]! = 0) { self.nameLabel.text = datasource.myname; Datasource.myname = @ ""; } */} @end
In this case, a singleton pattern is used, and the DataSource class is written, storing the data
datasource.h// testcallback//// Created by Csdc-imac on 14-7-17.// Copyright (c) 2014 Junewang. All rights reserved.//#import <Foundation/Foundation.h> @interface datasource:nsobject@property (nonatomic, Strong) NSString *myname;+ (datasource*) Shareddatasource; @end
datasource.m// testcallback//// Created by Csdc-imac on 14-7-17.// Copyright (c) 2014 Junewang. All rights reserved.//#import "DataSource.h" @implementation datasource+ (DataSource *) shareddatasource{ static DataSource *datasource = nil; static dispatch_once_t once; Dispatch_once (&once, ^{ dataSource = [DataSource new]; }); return dataSource;} @end
Program run
A view:
b View
When the name is entered and the corresponding confirmation button is clicked, it goes back to a view and displays the name entered in B view
Wish: Have fun, there is any other way or not the right place, please correct me.
If not written in detail, can be analyzed through the source code.
Reference: http://blog.csdn.net/cocoarannie/article/details/11857141
Http://www.cnblogs.com/heri/archive/2013/03/18/2965815.html
Source Address: Https://github.com/wangtao169447/PassValue