A.概念
1.也可以用来切换控制器
2.如ActionSheet
除了push之外,还有另外一种控制器的切换方式,那就是Modal
任何控制器都能通过Modal的形式展示出来
Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止
以Modal的形式展示控制器
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
关闭当初Modal出来的控制器
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;
以Modal的形式展示控制器
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
关闭当初Modal出来的控制器
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;
B.使用
1.使用代码
展示modal控制器: presentViewController
隐去modal界面:dismissViewController
源控制器和modal控制器的关系
- rootViewController一直都是源控制器
- 系统会自己替换view
- 源控制器中有一个属性指向modal控制器:presentedViewController(所以modal控制器才不会被销毁)
使用NavigationController包装一下modal控制器,添加上一个导航条
AppDelegate:
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 }
2.使用storyboard
(1)拖入相应控制器并连线
使用“modal”进行跳转,而不是之前的“push”
(2)关闭modal,回跳
GreenViewController:
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 }
(3)关于modal连线的数据传输问题
a.顺传:使用segue的控制器
“红”控制器传送字符串给“绿”控制器:
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 }
b.逆传:使用代理
“绿”控制器传送数据给“红”控制器