Modal模态
一、Modal介绍
除了push之外,还有另外一种控制器的切换方式,那就是Modal
任何控制器都能通过Modal的形式展示出来
Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止
二、Modal操作
以Modal的形式展示控制器
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
关闭当初Modal出来的控制器
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;
三、代码
WMOneViewController.m
//
// WMOneViewController.m
// 03_UIView40_Modal
//
// Created by 杞文明 on 2016/04/10 11:54:06 星期日
// Copyright © 2016年 杞文明. All rights reserved.
//
#import "WMOneViewController.h"
#import "WMTwoViewController.h"
@interface WMOneViewController ()
@end
@implementation WMOneViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)jump2Two:(id)sender {
//1.创建控制器
WMTwoViewController * vc = [[WMTwoViewController alloc]init];
//2.创建导航控制器
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc];
//3.模态方式打开控制器
[self presentViewController:nvc animated:YES completion:^{
NSLog(@"完全打开 第二个了");
}];
}
@end
WMTwoViewController.m
//
// WMTwoViewController.m
// 03_UIView40_Modal
//
// Created by 杞文明 on 2016/04/10 11:53:50 星期日
// Copyright © 2016年 杞文明. All rights reserved.
//
#import "WMTwoViewController.h"
#import "WMThreeViewController.h"
@interface WMTwoViewController ()
@end
@implementation WMTwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
//返回按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(closeTwo)];
}
/**
*跳到第三个界面
*/
- (IBAction)jump2Three:(id)sender {
//1.创建控制器
WMThreeViewController *vc = [[WMThreeViewController alloc]init];
//2.模态方式打开
[self presentViewController:vc animated:YES completion:^{
NSLog(@"完全打开 第三个了");
}];
}
-(void)closeTwo{
// 如果一个控制器是以模态的形式展现出来的, 可以调用该控制器以及该控制器的子控制器让让控制器消失
// [self dismissViewControllerAnimated:YES completion:^{
// NSLog(@"完全打开 第二个了");
// }];
[self.navigationController dismissViewControllerAnimated:YES completion:^{
NSLog(@"完全打开 第二个了");
}];
}
@end
WMThreeViewController.m
//
// WMThreeViewController.m
// 03_UIView40_Modal
//
// Created by 杞文明 on 2016/04/10 11:53:59 星期日
// Copyright © 2016年 杞文明. All rights reserved.
//
#import "WMThreeViewController.h"
@interface WMThreeViewController ()
@end
@implementation WMThreeViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
//关闭
- (IBAction)myClose:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"已经关闭 第三个了");
}];
}
@end