iOS 两个viewcontroller之间的delegate传值

来源:互联网 发布:知乎雅思阅读解题技巧 编辑:程序博客网 时间:2024/06/10 17:46

第二个界面往第一个界面传值

第二个界面

//第二个界面制定协议

@protocol ChuanZhiDelegate <NSObject>

- (void)chuanzhid;

@end

@interface ViewController2 :UIViewController

@property(nonatomic,assign)id<ChuanZhiDelegate>chuanzhidelegate;

@end


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor=[UIColorcyanColor];

    self.btn=[[UIButtonalloc]initWithFrame:CGRectMake(70,100, 100, 30)];

    self.btn.backgroundColor=[UIColorredColor];

    [self.viewaddSubview:self.btn];

    [self.btnaddTarget:selfaction:@selector(chuan)forControlEvents:UIControlEventTouchUpInside];

}

- (void)chuan{

   if ([self.chuanzhidelegaterespondsToSelector:@selector(chuanzhid)]) {

        [self.chuanzhidelegatechuanzhid];

    }else{

        NSLog(@"代理没有实现changeStatus:方法");

    }

}


然后第一个界面


- (void)viewDidLoad {

    [superviewDidLoad];

    self.btn=[[UIButtonalloc]initWithFrame:CGRectMake(70,100, 100, 30)];

    self.btn.backgroundColor=[UIColorredColor];

    [self.viewaddSubview:self.btn];

    [self.btnaddTarget:selfaction:@selector(chuanzhi)forControlEvents:UIControlEventTouchUpInside];

    self.view.backgroundColor=[UIColorlightGrayColor];

}

- (void)chuanzhi{

    ViewController2 *vc=[[ViewController2alloc]init];

    vc.chuanzhidelegate=self;//设置为第二个界面的代理

    [self.navigationControllerpushViewController:vc animated:YES];

}

//实现协议

- (void)chuanzhid{

    NSLog(@"代理实现");

}


0 1
原创粉丝点击