标签:
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 }
标签:
原文地址:http://www.cnblogs.com/kengsir/p/4281924.html