你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 兩個viewcontroller之間的delegate傳值

iOS 兩個viewcontroller之間的delegate傳值

編輯:IOS開發綜合

第二個界面往第一個界面傳值

第二個界面

 

//第二個界面制定協議

@protocol ChuanZhiDelegate

- (void)chuanzhid;

@end

@interface ViewController2 : UIViewController

@property(nonatomic,assign)idchuanzhidelegate;

@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(@"代理實現");

}


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved