[iOS基础控件 - 6.12.2] Modal
以Modal的形式展示控制器
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
关闭当初Modal出来的控制器
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;
- rootViewController一直都是源控制器
- 系统会自己替换view
- 源控制器中有一个属性指向modal控制器:presentedViewController(所以modal控制器才不会被销毁)
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 4 // 设置window 5 self.window = [[UIWindow alloc] init]; 6 self.window.backgroundColor = [UIColor grayColor]; 7 self.window.frame = [[UIScreen mainScreen] bounds]; 8 [self.window makeKeyAndVisible]; 9 10 // 创建一个UIView 11 UIViewController *viewController = [[UIViewController alloc] init]; 12 viewController.view.backgroundColor = [UIColor redColor]; 13 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 50)]; 14 [button setTitle:@"弹出modal" forState:UIControlStateNormal]; 15 [button addTarget:self action:@selector(popToModal) forControlEvents:UIControlEventTouchUpInside]; 16 17 [viewController.view addSubview:button]; 18 self.window.rootViewController = viewController; 19 20 return YES; 21 } 22 23 - (void) popToModal { 24 UIViewController *viewController = self.window.rootViewController; 25 26 // 再创建一个UIView用来切换 27 UIViewController *modalViewController = [[UIViewController alloc] init]; 28 modalViewController.view.backgroundColor = [UIColor blueColor]; 29 30 // 创建一个NavigationController来包装 31 UINavigationController *naviController = [[UINavigationController alloc] initWithRootViewController:modalViewController]; 32 modalViewController.navigationItem.title = @"蓝色"; 33 34 // 切换到navigationController 35 [viewController presentViewController:naviController animated:YES completion:^{ 36 NSLog(@"完成modal切换"); 37 }]; 38 }
1 - (IBAction)goback:(UIBarButtonItem *)sender { 2 // 关闭modal 3 // 1.可以使用自己 4 // [self dismissViewControllerAnimated:YES completion:nil]; 5 6 // 2.也可以使用上一层的navigation controller 7 [self.navigationController dismissViewControllerAnimated:YES completion:nil]; 8 }
1 /* 2 * 在这里传输数据给modal控制器 3 * 顺传,使用控制器 4 */ 5 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 6 // 1.取得modal转向的controller,这里是中间的navigation controller 7 UINavigationController *nvController = (UINavigationController *)segue.destinationViewController; 8 9 // 2.取得modal最终显示的controller, 就是最后的UIViewController 10 GreenViewController *greenController = (GreenViewController *)nvController.topViewController; 11 12 // 3.传输数据 13 greenController.name = @"我就是一个modal窗口"; 14 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。