在iOS8.3之后,apple废除了UIActionSheet,在iOS9中,apple废除了UIAlertView,使用了UIAlertController来替代,下面,我们来说说UIAlertController的使用方法:
1.创建一个UIAlertController对象
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"Test"message:@"This is a test"preferredStyle:UIAlertControllerStyleAlert];
preferredStyle:
UIAlertControllerStyleActionSheet 如果使用这个值,相当于创建一个UIActionSheet
UIAlertControllerStyleAlert 相当于创建一个UIAlertView
2.创建一个UIAlertAction对象
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
NSLog(@"test=======");//点击确定按钮之后需要执行的操作
}];
style:
UIAlertActionStyleDefault 默认样式
UIAlertActionStyleCancel 表明这个action取消了某些操作
UIAlertActionStyleDestructive 表明这个action可能会改变或删除数据
[alert addAction:action];
4.使用模态视图将alert显示出来
[self presentViewController:alert animated:YEScompletion:nil];
注意:若是将创建的过程直接写在viewDidLoad方法中,编译器会给出以下的警告:
Warning: Attempt to present <UIAlertController: 0x7fade969aae0> on <ViewController: 0x7fade967f560> whose view is not in the window hierarchy!
此处http://blog.csdn.net/sbvfhp/article/details/19826221提供了解决方案,即
在viewDidLoad里用
[self performSelectorOnMainThread:@selector(login) withObject:nil waitUntilDone:NO];
把页面跳转的代码写进函数里,然后将 waitUntilDone 设为NO,就是viewDidLoad直接返回不用等login执行。这样就可以确保login里的页面跳转是在viewDidLoad之后执行。