首页 > 移动平台 > 详细

iOS开发页面传值方案

时间:2015-11-17 17:08:14      阅读:311      评论:0      收藏:0      [点我收藏+]

iOS开发过程中涉及到最多的一个地方就是页面传值问题,值从A页面到B页面,然后从B页面到A页面。从而保证整个程序的连贯性和实时性。

目前iOS页面间传值方案有如下方式:

1.属性传值 2.委托delegate传值 3.通知notification传值 4.block传值 5.kvo传值 6.userDefault传值 7.文件传值 8.单例模式传值 9.数据库传值

1.属性传值:A页面设置属性 NSString *paramString,在跳转B页面的时候初始化paramString。

//A页面.h文件
@property (nonatomic, copy)NSString *paramString;

//A页面.m文件
 NextViewController *nextVC = [[NextViewController alloc] init];
 nextVC.paramString = @"参数传质";
 [self presentViewController:nextVC animated:YES completion:nil];

2.委托delegate传值:在B页面定义delegate,并且设置delegate属性,在A页面实现delegate协议

3.通知notification传值:在B页面中发送通知,在A页面注册观察者并且在不用的时候移除观察者。

//B页面发送通知 
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeNameNotification" object:self userInfo:@{@"name":self.nameTextField.text}];
 [self dismissViewControllerAnimated:YES completion:nil];

//A页面注册观察者
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@"ChangeNameNotification" object:nil];
}
//观察到通知时候的处理方法
-(void)ChangeNameNotification:(NSNotification*)notification{
    NSDictionary *nameDictionary = [notification userInfo];
    self.nameLabel.text = [nameDictionary objectForKey:@"name"];
}

//通知不使用的时候移除观察者
 [[NSNotificationCenter defaultCenter] removeObserver:self];

4.block传值:在B页面定义一个block类型的变量,在B页面跳转A的时候调用这个block。在A页面跳转到B页面的时候对B页面的block赋值。


//B页面定义block,并设置block类型的变量
 typedef void (^ablock)(NSString *str);
 @property (nonatomic, copy) ablock block;

//B页面跳转到A页面调用这个block
 self.block(self.nameTextField.text);
 [self dismissViewControllerAnimated:YES completion:nil];

//A页面跳转到B页面的时候对B页面的block赋值,这样在B页面跳转的时候就会回调这个block函数
 [self presentViewController:second animated:YES completion:nil];
 second.block = ^(NSString *str){
        self.nameLabel.text = str;
    };

5.kvo传值:在A页面设置B页面的变量second,并且对这个变量进行观察

- (void)addObserver:(NSObject * _Nonnull)anObserver forKeyPath:(NSString * _Nonnull)keyPath options:(NSKeyValueObservingOptions)options context:(void * _Nullable)context

并在A页面实现

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context方法。

在B页面对变量keyPath进行设置,在A页面都会观察的到。

@property (nonatomic, strong) SecondViewController *second; 
//在A视图跳转到B视图的地方添加如下代码 
self.second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.second addObserver:self forKeyPath:@"userName" options:NSKeyValueObservingOptionNew context:nil];
[self presentViewController:self.second animated:YES completion:nil];

//实现这个观察对象的方法
 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

//在B页面对userName进行设置,在A页面都可以间听到



6.UserDefault传值:保存本地

7.文件传值:保存到文件,从文件中读取

8.单例模式传值:通过全局的方式保存

9.数据库传值:保存到本地数据库,从数据库中读取。


iOS开发页面传值方案

原文:http://my.oschina.net/simple2012/blog/531609

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!