1.首先创建一个登录界面,在界面中有一个取消和一个登录按钮
#import”KCLoginViewController.h”
@interfaceKCLoginViewController ()
@end
@implementationKCLoginViewController
-(void)viewDidLoad {
[super viewDidLoad];
[self addLoginForm];
}
-(void)addLoginForm{
//登录按钮
UIButton *btnLogin=[UIButtonbuttonWithType:UIButtonTypeSystem];
btnLogin.frame=CGRectMake(120, 270, 80,30);
[btnLoginsetTitle:@”登录”forState:UIControlStateNormal];
[self.viewaddSubview:btnLogin];
//取消登录按钮
UIButton*btnCancel=[UIButtonbuttonWithType:UIButtonTypeSystem];
btnCancel.frame=CGRectMake(170, 270, 80,30);
[btnCancel setTitle:@”取消”forState:UIControlStateNormal];
[self.view addSubview:btnCancel];
[btnCancel addTarget:selfaction:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
}
-(void)cancel
{
[self dismissViewControllerAnimated:YEScompletion:nil];//点击取消
}
@end
2.定义主界面视图控制器KCMainViewController,在左上角放一个登录按钮用于弹出登录界面
#import”KCMainViewController.h”
#import”KCLoginViewController.h”
@interfaceKCMainViewController()<KCMainDelegate,UIActionSheetDelegate>
{
UIBarButtonItem*_loginButton;
BOOL _isLogin;
}
@end
@implementationKCMainViewController
-(void)viewDidLoad {
[super viewDidLoad];
[self addNavigationBar];
}
#pragmamark 添加到导航栏
-(void)addNavigationBar
{
UINavigationBar *navigationBar = [[UINavigationBaralloc]initWithFrame:CGRectMake(0,0, 320, 44+20)];
[self.viewaddSubview:navigationBar];
UINavigationItem *navigationItem = [[UINavigationItemalloc]initWithTitle:@”WebChat”];
//左侧添加登录按钮
_loginButton=[[UIBarButtonItemalloc]initWithTitle:@”登录”style:UIBarButtonItemStyleDonetarget:selfaction:@selector(login)];
navigationItem.leftBarButtonItem=_loginButton;
[navigationBarpushNavigationItem:navigationItemanimated:NO];
}
#pragmamark 登录操作
-(void)login
{
KCLoginViewController *loginController = [[KCLoginViewControlleralloc]init];
loginController.delegate =self;
[self presentViewController:loginControlleranimated:YEScompletion:nil];
}
@end
AppDelegate.m中
_window=[[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
_window.backgroundColor =[UIColorcolorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
KCMainViewController *mainController = [[KCMainViewControlleralloc]init];
_window.rootViewController = mainController; //把KCMainViewController设置为根控制器
[_window makeKeyAndVisible];
今天的文章presentModalViewController的使用方法分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/12563.html