模态窗口只是视图控制器显示的一种方式(在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);方法就会关闭模态窗口。
#import "KCLoginViewController.h”
@interface KCLoginViewController ()
@end
@implementation KCLoginViewController
-(void)viewDidLoad {
[super viewDidLoad];
[self addLoginForm];
}
-(void)addLoginForm{
//登录按钮
UIButton *btnLogin=[UIButton buttonWithType:UIButtonTypeSystem];
btnLogin.frame=CGRectMake(120, 270, 80, 30);
[btnLogin setTitle:@"登录" forState:UIControlStateNormal];
[self.view addSubview:btnLogin];
//取消登录按钮
UIButton *btnCancel=[UIButton buttonWithType:UIButtonTypeSystem];
btnCancel.frame=CGRectMake(170, 270, 80, 30);
[btnCancel setTitle:@"取消" forState:UIControlStateNormal];
[self.view addSubview:btnCancel];
[btnCancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
}
-(void)cancel
{
[self dismissViewControllerAnimated:YES completion:nil];//点击取消
}
@end
1111111111111111111111
#import "KCMainViewController.h"
#import "KCLoginViewController.h”
@interface KCMainViewController ()<KCMainDelegate,UIActionSheetDelegate>
{
UILabel *_loginInfo;
UIBarButtonItem *_loginButton;
BOOL _isLogin;
}
@end
@implementation KCMainViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addNavigationBar];
[self addLoginInfo];
}
-(void)addLoginInfo
{
_loginInfo = [[UILabel alloc]initWithFrame:CGRectMake(0, 100,320 ,30)];
_loginInfo.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_loginInfo];
}
#pragma mark 添加到导航栏
-(void)addNavigationBar
{
UINavigationBar *navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44+20)];
[self.view addSubview:navigationBar];
UINavigationItem *navigationItem = [[UINavigationItem alloc]initWithTitle:@"Web Chat"];
//左侧添加登录按钮
_loginButton=[[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStyleDone target:self action:@selector(login)];
navigationItem.leftBarButtonItem=_loginButton;
[navigationBar pushNavigationItem:navigationItem animated:NO];
}
宋鸿业 21:21:18
-(void)login
{
KCLoginViewController *loginController = [[KCLoginViewController alloc]init];
loginController.delegate = self;
[self presentViewController:loginController animated:YES completion:nil];
}
@end