模态 viewController :
介绍:
程序中切换⻚面,可以使⽤ UINavigationController 。通过导航功能实现⻚面切换。
某种情况下,可以使⽤视图控制器的⼀对⽅法实现切换⻚面
presentViewController:animated:completion: 展⽰新的⻚面
dismissViewControllerAnimated:completion: 从新⻚面返回
通过上述⽅法显⽰的 controller 被称为模态视图控制器。
注:模态视图控制器不是
⼀个类,只是⼀种⽅式显示的 controller 。
使用场景:
UIImagePickerController。调⽤系统相册、照相机。
临时展⽰一些内容。例如:程序中⽤户登录,通讯录中添加联系人,弹出的临时广告页等等。
导航控制器控制的 controller 具有层级关系。present 控制显示的 controller 与之前的 controller 是两个层级。
使用方式:
presentViewController:animated:completion: 模态显⽰
controller 在 controllerA 中模态显⽰ controllerB
presenting view controller => controllerA
presented view controller => controllerB ( 被弹出的 )
dismissViewControllerAnimated:completion: 弹出模态 controller
两种实现方式,在模态 controller 中使⽤:
[self dismissViewControllerAnimated:YES completion: nil];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
使⽤self调用⽅法,系统会使⽤ self.presentingViewController 调⽤这个方法。
属性设置
modalPresentationStyle 模态 controller 显⽰样式
modalTransitionStyle 模态显⽰动画样式
RootViewController.m
NextViewController.m
介绍:
程序中切换⻚面,可以使⽤ UINavigationController 。通过导航功能实现⻚面切换。
某种情况下,可以使⽤视图控制器的⼀对⽅法实现切换⻚面
presentViewController:animated:completion: 展⽰新的⻚面
dismissViewControllerAnimated:completion: 从新⻚面返回
通过上述⽅法显⽰的 controller 被称为模态视图控制器。
注:模态视图控制器不是
⼀个类,只是⼀种⽅式显示的 controller 。
使用场景:
UIImagePickerController。调⽤系统相册、照相机。
临时展⽰一些内容。例如:程序中⽤户登录,通讯录中添加联系人,弹出的临时广告页等等。
导航控制器控制的 controller 具有层级关系。present 控制显示的 controller 与之前的 controller 是两个层级。
使用方式:
presentViewController:animated:completion: 模态显⽰
controller 在 controllerA 中模态显⽰ controllerB
presenting view controller => controllerA
presented view controller => controllerB ( 被弹出的 )
dismissViewControllerAnimated:completion: 弹出模态 controller
两种实现方式,在模态 controller 中使⽤:
[self dismissViewControllerAnimated:YES completion: nil];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
使⽤self调用⽅法,系统会使⽤ self.presentingViewController 调⽤这个方法。
属性设置
modalPresentationStyle 模态 controller 显⽰样式
modalTransitionStyle 模态显⽰动画样式
AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController * rootVC = [[RootViewController alloc]init];
UINavigationController * rootNC = [[UINavigationController alloc]initWithRootViewController:rootVC];
self.window.rootViewController = rootNC;
return YES;
}
@end
RootViewController.m
#import "RootViewController.h"
#import "NextViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NextViewController * nextVC = [[NextViewController alloc]init];
//模态推出视图
//模态直接用controller就可以直接推出,它没有层级结构
[self presentViewController:nextVC animated:YES completion:^{
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NextViewController.m
#import "NextViewController.h"
@interface NextViewController ()
@end
@implementation NextViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//取消模态视图
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end