1.在iOS中正向传值 可以用属性就不多说了,反向传值 从image中传值给controller的时候 我们可以用 代理 Block代码块 还有通知的方法传值 举个例子: A页面push到B页面 现在要从B页面pop回 A页面的时候传值给 A页面
(1).简单说就是从B页面传值给A页面 可以用block传值 在B页面的.h中声明block的函数 typedef void(^SelectedBlock)(NSString *textname);
@interface textTwoVC : UIViewController
@property (nonatomic, copy) SelectedBlock selectedBlock;
typedef void 是声明一个新的类型 SelectedBlock 是block函数的名字 textname 是传值的内容 当然可以传递多个参数
(2)然后在B页面的.m中 进行传值操作 if (self.selectedBlock) { self.selectedBlock(@"传值123"); } 判断一个block是否存在 在进行传值
(3)在A页面中接受B页面传过来的值 在A页面需要取值的时候 _texttwoVC.selectedBlock=^(NSString *textStr){ //textStr 就是传过来的值 }; 2.通过通知传值 大体的套路和block差不多 就是创建通知中心 进行通知传值 接收通知传来的值 (1) //获取通知中心单例对象 NSNotificationCenter * center = [NSNotificationCenter defaultCenter]; //添加当前类对象为一个观察者,name和object设置为nil,表示接收一切通知 给object设置名字 只接受和object名字匹配的通知消息 [center addObserver:self selector:@selector(notice:) name:@"ceshi" object:nil]; (2) //创建一个消息对象 NSNotification * notice = [NSNotification notificationWithName:@"ceshi" object:nil userInfo:@{@"1":@"123"}]; //发送消息 [[NSNotificationCenter defaultCenter]postNotification:notice]; (3) //在回调的函数中取出通知的值 -(void)notice:(id)sender{ NSLog(@"%@",sender); }