模态窗口
模态窗口只是视图控制器显示的一种方式(在iOS中并没有专门的模态窗口类),模态窗口不依赖于控制器容器(例如前两种视图切换一个依赖于 UITabBarController,另一个依赖于UINavigationController),通常用于显示独立的内容,在模态窗口显示的时其他 视图的内容无法进行操作。
模态窗口使用起来比较容易,一般的视图控制器只要调用- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0);方法那么参数中的视图控制器就会以模态窗口的形式展现,同时调用- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion NS_AVAILABLE_IOS(5_0);方法就会关闭模态窗口。
下面的示例中演示了一个登录操作,点击主界面左上方登录按钮以模态窗口的形式展现登录界面,用户点击登录界面中的登录按钮就会返回到主界面。特别强 调一点在下面的示例中导航栏是手动创建的,而不是采用UINavigationController,为了帮助大家熟悉导航栏使用同时也了解了 UInavigationController中导航栏的本质。
1.首先创建一个登录界面,在界面中只有两个输入框和一个登录按钮
- //
- // KCLoginViewController.m
- // ViewTransition
- //
- // Created by Kenshin Cui on 14-3-15.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCLoginViewController.h"
- @interface KCLoginViewController ()
- @end
- @implementation KCLoginViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self addLoginForm];
- }
- -(void)addLoginForm{
- //用户名
- UILabel *lbUserName=[[UILabel alloc]initWithFrame:CGRectMake(50, 150, 100, 30)];
- lbUserName.text=@"用户名:";
- [self.view addSubview:lbUserName];
- UITextField *txtUserName=[[UITextField alloc]initWithFrame:CGRectMake(120, 150, 150, 30)];
- txtUserName.borderStyle=UITextBorderStyleRoundedRect;
- [self.view addSubview:txtUserName];
- //密码
- UILabel *lbPassword=[[UILabel alloc]initWithFrame:CGRectMake(50, 200, 100, 30)];
- lbPassword.text=@"密码:";
- [self.view addSubview:lbPassword];
- UITextField *txtPassword=[[UITextField alloc]initWithFrame:CGRectMake(120, 200, 150, 30)];
- txtPassword.secureTextEntry=YES;
- txtPassword.borderStyle=UITextBorderStyleRoundedRect;
- [self.view addSubview:txtPassword];
- //登录按钮
- UIButton *btnLogin=[UIButton buttonWithType:UIButtonTypeSystem];
- btnLogin.frame=CGRectMake(120, 270, 80, 30);
- [btnLogin setTitle:@"登录" forState:UIControlStateNormal];
- [self.view addSubview:btnLogin];
- [btnLogin addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
- }
- #pragma mark 登录操作
- -(void)login{
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- @end
2.定义主界面视图控制器KCMainViewController,在左上角放一个登录按钮用于弹出登录界面
- //
- // KCMainViewController.m
- // ViewTransition
- //
- // Created by Kenshin Cui on 14-3-15.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCMainViewController.h"
- #import "KCLoginViewController.h"
- @interface KCMainViewController ()
- @end
- @implementation KCMainViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self addNavigationBar];
- }
- #pragma mark 添加导航栏
- -(void)addNavigationBar{
- //创建一个导航栏
- UINavigationBar *navigationBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44+20)];
- //navigationBar.tintColor=[UIColor whiteColor];
- [self.view addSubview:navigationBar];
- //创建导航控件内容
- UINavigationItem *navigationItem=[[UINavigationItem alloc]initWithTitle:@"Web Chat"];
- //左侧添加登录按钮
- UIBarButtonItem *loginButton=[[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStyleDone target:self action:@selector(login)];
- navigationItem.leftBarButtonItem=loginButton;
- //添加内容到导航栏
- [navigationBar pushNavigationItem:navigationItem animated:NO];
- }
- #pragma mark 登录操作
- -(void)login{
- KCLoginViewController *loginController=[[KCLoginViewController alloc]init];
- //调用此方法显示模态窗口
- [self presentViewController:loginController animated:YES completion:nil];
- }
- @end