- iOS 8之后苹果推荐我们使用的系统自带的提示框是UIAlertController。UIAlertController有两种类型:
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
它的出现是为了替带之前的UIAlertView和UIActionSheet两个类。 -
下面介绍一下UIAlertController,顺便有示例。
- 1.登录型的提示框
- (IBAction)loginAlert:(UIButton *)sender {
//获取用户当前手机的系统版本
[[UIDevice currentDevice] systemVersion];
//当前的系统版本大于等于8.0
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
//1.初始化action
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//找到输入框对象
UITextField *usernameTF = alertController.textFields[0];
UITextField *passwordTF = alertController.textFields[1];
//取出用户名和密码
NSString *username = usernameTF.text;
NSString *password = passwordTF.text;
NSLog(@"username:%@\npassword:%@",username,password);
}];
//2.添加动作
[alertController addAction:action1];
[alertController addAction:action2];
//添加输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入用户名";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入密码";
textField.secureTextEntry = YES;
}];
//显示
[self presentViewController:alertController animated:YES completion:nil];
}
}
-
2.UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet类型的,这里的示例是传入一个类型,然后根据类型展示不同风格。
- (void)btnTouchWithStyle:(UIAlertControllerStyle)style{
/*
preferredStyle:
1.UIAlertControllerStyleActionSheet
2.UIAlertControllerStyleAlert
*/
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
//iOS8以后才有的,替换了UIAlertView和UIActionSheet两个类
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"分享" message:@"选择分享的平台" preferredStyle:style];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消按钮点击");
}];
UIAlertAction *qqAction = [UIAlertAction actionWithTitle:@"分享到QQ" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"分享到QQ");
}];
UIAlertAction *weixinAction = [UIAlertAction actionWithTitle:@"分享到微信" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"分享到微信");
}];//添加动作 [alertController addAction:cancelAction]; [alertController addAction:qqAction]; [alertController addAction:weixinAction]; //显示 [self presentViewController:alertController animated:YES completion:nil]; }else{ NSLog(@"当前的系统版本是小于8.0的,使用UIAlertView"); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享" message:@"选择分享的平台" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alertView show]; } NSLog(@"------当前的系统版本: %f---",[[[UIDevice currentDevice] systemVersion] floatValue]); }