前面传后面的常用的是属性传值,后面传前面的有两种方法,第一种是协议传值,自己声明协议声明代理来完成,第二种是block传值,但是协议传值只能是有关联的传值,没有关联的传值是不行的,block就可以无关联的传值。
下面我们来看下属性传值,我们需要先创建一个UIButton放到view里面。必须得有点击事件。
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"你被骗了" forState:UIControlStateNormal];
button.frame = CGRectMake(100, 200, 100, 30); [button addTarget:self action:@selector(goTo:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
然后我们需要实现这个点击事件。
- (void)goTo:(UIButton *)slider { SecondViewController *second = [[SecondViewController alloc] init];
// 给推送的那界面的赋值 second.string = self.textField.text; second.color = [UIColor redColor]; [self.navigationController pushViewController:second animated:YES]; }
既然后赋值,那第二张推送过来的界面肯定需要一个属性来接受这个传过来的值。
// 声明一个字符创属性来保存由第一个界面传过来的字符串内容 @property (nonatomic, copy) NSString *string;
然后在 .m 中我们需要用一个 UILabel 来接收。才能在点击跳转时展示出来。
_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)]; _label.text = self.string; _label.textAlignment = NSTextAlignmentCenter; _label.font = [UIFont boldSystemFontOfSize:30]; _label.textColor = self.color; [self.view addSubview:_label];
这样,属性传值就搞定了。接下来就是协议传值了。首先,我们需要声明一个协议。
// 1、声明协议 // UI中得协议名称为 当前类名 + Delegate @protocol FourthViewControllerDelegate <NSObject> - (void)pushValue:(NSString *)text; @end
然后就是第二步,声明代理。
// 2、声明代理 @property (nonatomic, assign) id<FourthViewControllerDelegate>delegate;
在 .m 中,我们要添加一个UITextField用来输入传出的值,再添加一个UIButton,点击后跳转界面。
_textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, kWidth - 200, 30)]; _textField.placeholder = @"请输入文本"; _textField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:_textField]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"点我啊" forState:UIControlStateNormal]; [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(150, 160, 100, 30); [self.view addSubview:button];
实现UIButton的点击事件。
- (void)back { // 这个判断必须要,用来判断是否为空 if (self.delegate != nil && [self.delegate respondsToSelector:@selector(pushValue:)]) { [self.delegate pushValue:self.textField.text]; [self.delegate pushColor:[UIColor redColor]]; } [self.navigationController popViewControllerAnimated:YES]; }
然后只要在返回的那个界面接收<FourthViewControllerDelegate>协议。在第一个界面的button点击时间中添加代理,并实现代理方法就完成了代理传值。
- (void)goTo { FourthViewController *fourth = [[FourthViewController alloc] init]; fourth.delegate = self; // 5、指定第二个界面的代理 [self.navigationController pushViewController:fourth animated:YES]; [fourth release]; } // 6 实现代理方法 - (void)pushValue:(NSString *)text { self.label.text = text; }
最后我们来讲下block传值。在第二个界面的 .h 里面,定义一个Block。
typedef void (^TestBlock)(NSString *);
接下来需要把Block声明属性。
// Block声明称属性,一定要使用copy @property (nonatomic, copy) TestBlock testBlock;
后面的方法跟前面协议传值一样,先添加UITextField跟UIButton,并实现UIButton的点击事件。
- (void)back { // 执行Block if (self.textBlock != nil) { self.textBlock(self.textField.text); } [self.navigationController popViewControllerAnimated:YES]; }
然后我们回到第一个页面的 .m 里面实现这个Block方法。
- (void)back { SecondViewController *second = [[SecondViewController alloc] init]; second.bada = ^(NSString *str) { self.label.text = str; }; }
这样,这三种最常见的传值我们就完成了。