iOS页面间传值实现方法:1.通过设置属性,实现页面间传值;2.委托delegate方式;3.通知notification方式;4.block方式;5.UserDefault或者文件方式;6.单例模式方式(类似UserDefault,不再赘述)

 

1.通过设置属性,实现页面间传值(最简单的一种方式,常用来进行单向传值)

在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给B的属性赋值即可

1.//SecondViewController.h
view sourceprint?
1.@property(nonatomic) NSInteger flag;//当前系统标示(0:其他传值方式;1:block传值方式)

在A页面的试图控制器中

1.//RootViewController.m
view sourceprint?
1.- (IBAction)showSecondView:(id)sender {
2.SecondViewController *second = [[SecondViewController alloc] initWithNibName:@'SecondViewController'bundle:nil];
3.second.delegate = self;
4.second.flag = 0;
5.[self presentViewController:second animated:YES completion:nil];
6.}

 

2.委托delegate方式(两个页面之间双向传值)

A页面跳转到B页面,B页面再跳转回A页面

\

 

 设置协议及方法

1.
@protocol secondViewDelegate
2.
-(void)showName:(NSString *)nameString;
3.
@end

设置代理(为防止循环引用,此处采用了weak)

1. 
view sourceprint?
1.//SecondViewController.h
view sourceprint?
1.@interface SecondViewController : UIViewController
2.@property (nonatomic, weak)id<secondViewDelegate> delegate;
3.@property (nonatomic, copy) ablock block;
4.@end

   调用

01.//SecondViewController.m
02.- (IBAction)delegateMethod:(id)sender {
03.if ([self notEmpty]) {
04.[self.delegate showName:self.nameTextField.text];
05.[self dismissViewControllerAnimated:YES completion:nil];
06.}else{
07.[self showAlert];
08.}
09.}

显示  

1.//RootViewController.m
2.-(void)showName:(NSString *)nameString{
3.self.nameLabel.text = nameString;
4.}

最重要也是最容易忽略的,就是一定要设置delegate的指向。

 

3.通知notification方式(两个页面之间双向传值)

A页面跳转到B页面,B页面再跳转回A页面

在B页面的控制器中,发送通知:

01.//SecondViewController.m
02.- (IBAction)notificationMethod:(id)sender {
03.if ([self notEmpty]) {
04.[[NSNotificationCenter defaultCenter] postNotificationName:@'ChangeNameNotification' object:self userInfo:@{@'name':self.nameTextField.text}];
05.[self dismissViewControllerAnimated:YES completion:nil];
06.}else{
07.[self showAlert];
08.}
09.}

在A页面的控制器中,注册通知:

1.//RootViewController.m
2.- (void)viewDidLoad
3.{
4.[super viewDidLoad];
5.// Do any additional setup after loading the view from its nib.
6.[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@'ChangeNameNotification' object:nil];
7.}

当我们不使用时,要记得删掉通知:

1.//RootViewController.m
2.-(void)dealloc{
3.[[NSNotificationCenter defaultCenter] removeObserver:self];
4.}

 调用,显示

1.//RootViewController.m
2. 
3.-(void)ChangeNameNotification:(NSNotification*)notification{
4.NSDictionary *nameDictionary = [notification userInfo];
5.self.nameLabel.text = [nameDictionary objectForKey:@'name'];
6.}

 

 

4.block方式(回调)

在B试图控制器中,定义一个block,参数为字符串

1.//SecondViewController.h
2.typedef void (^ablock)(NSString *str);
view sourceprint?
1.//SecondViewController.h
2. 
3.@property (nonatomic, copy) ablock block;

在B试图控制器中,当输入名字,点击对应的确定按钮后

01.- (IBAction)blockMethod:(id)sender {
02.if ([self notEmpty]) {
03.if (self.block) {
04.self.block(self.nameTextField.text);
05.[self dismissViewControllerAnimated:YES completion:nil];
06.}
07.}else{
08.[self showAlert];
09.}
10.}

在A试图显示,回调block

1.- (IBAction)showSecondWithBlock:(id)sender {
2.SecondViewController *second = [[SecondViewController alloc] initWithNibName:@'SecondViewController'bundle:nil];
3.[self presentViewController:second animated:YES completion:nil];
4.second.block = ^(NSString *str){
5.self.nameLabel.text = str;
6.};
7.}

 

5.UserDefault或者文件方式;6.单例模式方式
设置
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLead"] ;

取值

[NSUserDefaults standardUserDefaults] boolForKey:@"firstLead"]

 

posted on 2016-03-18 10:42  fatal-奚山遇白  阅读(147)  评论(0编辑  收藏  举报