方法一:使用target-action設計模式
榮昌ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!代碼如下:(由根視圖推出子視圖,再由子視圖推出根視圖,在推出根視圖時,子視圖傳一個color的屬性給根視圖,用來修改根視圖的背景顏色)
根視圖控制器代碼:
//.m文件 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; [self createButton]; } - (void)createButton { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(10, 30, 300, 40); [btn setTitle:@"進入下一個視圖控制器" forState:UIControlStateNormal]; btn.layer.cornerRadius = 5; btn.backgroundColor = [UIColor blackColor]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick { Sub1ViewController * sub1 = [[Sub1ViewController alloc]init]; sub1.view.backgroundColor = [UIColor blueColor]; sub1.target = self; sub1.action = @selector(changeColor:); [self presentViewController:sub1 animated:YES completion:nil]; } - (void)changeColor:(UIColor *)color { self.view.backgroundColor = color; }
子視圖代碼:
//.h文件 @interface Sub1ViewController : UIViewController @property (assign,readwrite,nonatomic)id target; @property (assign,readwrite,nonatomic)SEL action; @end //.m文件 - (void)viewDidLoad { [super viewDidLoad]; [self createPopToRootViewBtn]; } - (void)createPopToRootViewBtn { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(10, 30, 300, 40); [btn setTitle:@"進入根視圖控制器" forState:UIControlStateNormal]; btn.layer.cornerRadius = 5; btn.backgroundColor = [UIColor blackColor]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick { if ([_target respondsToSelector:_action]) { [_target performSelector:_action withObject:[UIColor orangeColor]]; } [self dismissViewControllerAnimated:YES completion:nil]; }
方法二:通知
//.m根視圖 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; [self createButton]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"ChangeColor" object:nil]; } - (void)createButton { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(10, 30, 300, 40); [btn setTitle:@"進入下一個視圖控制器" forState:UIControlStateNormal]; btn.layer.cornerRadius = 5; btn.backgroundColor = [UIColor blackColor]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick { Sub1ViewController * sub1 = [[Sub1ViewController alloc]init]; sub1.view.backgroundColor = [UIColor blueColor]; [self presentViewController:sub1 animated:YES completion:nil]; } - (void)changeColor:(NSNotification *)nofi { self.view.backgroundColor = [nofi.userInfo objectForKey:@"color"]; }
子視圖代碼
- (void)viewDidLoad { [super viewDidLoad]; [self createPopToRootViewBtn]; } - (void)createPopToRootViewBtn { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(10, 30, 300, 40); [btn setTitle:@"進入根視圖控制器" forState:UIControlStateNormal]; btn.layer.cornerRadius = 5; btn.backgroundColor = [UIColor blackColor]; [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeColor" object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor],@"color", nil]]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick { [self dismissViewControllerAnimated:YES completion:nil]; }
方法三:代碼塊
根視圖代碼:
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; [self createButton]; } - (void)createButton { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(10, 30, 300, 40); [btn setTitle:@"進入下一個視圖控制器" forState:UIControlStateNormal]; btn.layer.cornerRadius = 5; btn.backgroundColor = [UIColor blackColor]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick { Sub1ViewController * sub1 = [[Sub1ViewController alloc]init]; sub1.view.backgroundColor = [UIColor blueColor]; sub1.MyBlock = ^(UIColor * color) { self.view.backgroundColor = color; }; [self presentViewController:sub1 animated:YES completion:nil]; }
子視圖代碼
//.h文件 @interface Sub1ViewController : UIViewController @property (copy,nonatomic,readwrite)void (^MyBlock)(UIColor * color); @end //.m文件 - (void)viewDidLoad { [super viewDidLoad]; [self createPopToRootViewBtn]; } - (void)createPopToRootViewBtn { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(10, 30, 300, 40); [btn setTitle:@"進入根視圖控制器" forState:UIControlStateNormal]; btn.layer.cornerRadius = 5; btn.backgroundColor = [UIColor blackColor]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick { _MyBlock([UIColor orangeColor]); [self dismissViewControllerAnimated:YES completion:nil]; }
方法四:代理-協(xié)議
根視圖代碼
//.h #import <UIKit/UIKit.h> #import "Sub1ViewController.h" @interface ViewController : UIViewController<Sub1ViewControllerDelete> @end //.m - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; [self createButton]; } - (void)createButton { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(10, 30, 300, 40); [btn setTitle:@"進入下一個視圖控制器" forState:UIControlStateNormal]; btn.layer.cornerRadius = 5; btn.backgroundColor = [UIColor blackColor]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick { Sub1ViewController * sub1 = [[Sub1ViewController alloc]init]; sub1.view.backgroundColor = [UIColor blueColor]; sub1.delegate = self; [self presentViewController:sub1 animated:YES completion:nil]; } - (void)changeColor:(UIColor *)color { self.view.backgroundColor = color; }
子視圖控制器
//.h文件 #import <UIKit/UIKit.h> @protocol Sub1ViewControllerDelete <NSObject> - (void)changeColor:(UIColor *)color; @end @interface Sub1ViewController : UIViewController @property (assign,nonatomic,readwrite)id <Sub1ViewControllerDelete>delegate; @end //.m文件 - (void)viewDidLoad { [super viewDidLoad]; [self createPopToRootViewBtn]; } - (void)createPopToRootViewBtn { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(10, 30, 300, 40); [btn setTitle:@"進入根視圖控制器" forState:UIControlStateNormal]; btn.layer.cornerRadius = 5; btn.backgroundColor = [UIColor blackColor]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick { [_delegate changeColor:[UIColor orangeColor]]; [self dismissViewControllerAnimated:YES completion:nil]; }
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文題目:IOS四種反向傳值的方法-創(chuàng)新互聯(lián)
當前鏈接:http://m.rwnh.cn/article48/ddodep.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設計公司、網(wǎng)站內鏈、商城網(wǎng)站、響應式網(wǎng)站、ChatGPT、服務器托管
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容