上一篇博客中有用到UIAlertView和UIActionSheet,鉴于在iOS8中被废弃,所以简单说明下心提供的UIAlertController的用法,不过也不用急着换,还是可以正常使用的。

UIAlertController简单使用_封装


博主只说明三个简单的用法,

第一:UIAlertView
直接看代码:

- (void)AlertView
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"这是一个UIAlertController,UIAlertView已经废弃了." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
//取消操作
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//确定操作
}];
[alertVC addAction:cancelAction];
[alertVC addAction:okAction];
[self presentViewController:alertVC animated:YES completion:nil];

}

第二:UIActionSheet

- (void)ActionSheet
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"这是一个UIAlertController,UIActionSheet已经废弃了." preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
//取消操作
}];
//UIAlertActionStyleDestructive有着重的意思,使用这个常量标题变为红色
UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"第一个" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
//确定操作
}];
UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"第二个" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//确定操作
}];
[alertVC addAction:cancelAction];
[alertVC addAction:firstAction];
[alertVC addAction:secondAction];
[self presentViewController:alertVC animated:YES completion:nil];

}

第三:带输入框的UIAlertView

- (void)AlertTextField
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"登陆" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
//取消操作
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
//确定操作
//也可在此判断输入位数是否符合要求
NSLog(@"%@",loginTextField.text);
NSLog(@"%@",passTextField.text);
}];
[alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"登录";
}];
[alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
loginTextField = alertVC.textFields.firstObject;
passTextField = alertVC.textFields.lastObject;

[alertVC addAction:cancelAction];
[alertVC addAction:okAction];
[self presentViewController:alertVC animated:YES completion:nil];
}

用法也不难,看一下就好,博主写的只是基础用法,还有其他一些用法,不是很常用,所以就不再说明了,用到的话就自己查查吧,除了这么写外,还可以自定义,博主前面曾经封装过一个自定义的UIAlertView,想看的去“控件封装”分类中查看。网上也有更详细的介绍,博主这也是班门弄斧了。Demo下载地址就不放上去了,上面的方法加一个触发方式就好了。