十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
方法一:使用target-action设计模式
荣昌ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信: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]; }
方法四:代理-协议
根视图代码
//.h #import#import "Sub1ViewController.h" @interface ViewController : UIViewController @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@protocol Sub1ViewControllerDelete - (void)changeColor:(UIColor *)color; @end @interface Sub1ViewController : UIViewController @property (assign,nonatomic,readwrite)id 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]; }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。