iOS 两个viewcontroller之间的delegate传值
第二个界面往第一个界面传值
第二个界面
//第二个界面制定协议
@protocol ChuanZhiDelegate <NSObject>
- (void)chuanzhid;
@end
@interface ViewController2 : UIViewController
@property(nonatomic,assign)id<ChuanZhiDelegate>chuanzhidelegate;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor cyanColor];
self.btn=[[UIButton alloc]initWithFrame:CGRectMake(70, 100, 100, 30)];
self.btn.backgroundColor=[UIColor redColor];
[self.view addSubview:self.btn];
[self.btn addTarget:self action:@selector(chuan) forControlEvents:UIControlEventTouchUpInside];
}
- (void)chuan{
if ([self.chuanzhidelegate respondsToSelector:@selector(chuanzhid)]) {
[self.chuanzhidelegate chuanzhid];
}else{
NSLog(@"代理没有实现changeStatus:方法");
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.btn=[[UIButton alloc]initWithFrame:CGRectMake(70, 100, 100, 30)];
self.btn.backgroundColor=[UIColor redColor];
[self.view addSubview:self.btn];
[self.btn addTarget:self action:@selector(chuanzhi) forControlEvents:UIControlEventTouchUpInside];
self.view.backgroundColor=[UIColor lightGrayColor];
}
- (void)chuanzhi{
ViewController2 *vc=[[ViewController2 alloc]init];
vc.chuanzhidelegate=self;//设置为第二个界面的代理
[self.navigationController pushViewController:vc animated:YES];
}
//实现协议
- (void)chuanzhid{
NSLog(@"代理实现");
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。