运行环境:
Xcode7.2.1,iOS Simulator9.2
语言:
Objective-C、Swift
关于UIAlertController的使用,主要有三种不同的方式:
1.简单的UIAlertController形式;
![简单的UIAlertController形式](https://img-blog.csdn.net/20160509171509484)
2.带有输入框的UIAlertController;
![带有输入框的UIAlertController](https://img-blog.csdn.net/20160509171629438)
3.ActionSheet样式的UIAlertController。
![ActionSheet样式的UIAlertController](https://img-blog.csdn.net/20160509171646745)
下面依次说明一下三种方式的创建和使用。
1.简单的UIAlertController形式
Objective-C 版
UIAlertController *alertOne = [UIAlertController alertControllerWithTitle:@"Alert-1" message:@"这是第一个AlertController" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
}];
[alertOne addAction:cancelAction];
[alertOne addAction:confirmAction];
[self presentViewController:alertOne animated:YES completion:nil];
Swift 版
let alertOne = UIAlertController.init(title: "Alert-1", message: "这是第一个AlertController", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
let confirmAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default){ (action: UIAlertAction) -> Void in
}
alertOne.addAction(cancelAction)
alertOne.addAction(confirmAction)
self.presentViewController(alertOne, animated: true, completion: nil)
2.带有输入框的UIAlertController
Objective-C 版
UIAlertController *alertTwo = [UIAlertController alertControllerWithTitle:@"Alert-2" message:@"这是第二个AlertController" preferredStyle:UIAlertControllerStyleAlert];
[alertTwo addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textfield){
textfield.placeholder = @"第一个输入框";
textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
}];
[alertTwo addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textfield){
textfield.placeholder = @"第二个输入框";
textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
NSLog([alertTwo.textFields[0] text]);
NSLog([alertTwo.textFields[1] text]);
}];
[alertTwo addAction:cancelAction];
[alertTwo addAction:confirmAction];
[self presentViewController:alertTwo animated:YES completion:nil];
Swift 版
// 创建一个UIAlertController,命名为alertTwo
let alertTwo = UIAlertController.init(title: "Alert-2", message: "这是第二个AlertController", preferredStyle: UIAlertControllerStyle.Alert)
// 给alertTwo添加第一个输入框
alertTwo.addTextFieldWithConfigurationHandler{ (textfield: UITextField) -> Void in
textfield.placeholder = "第一个输入框"
}
// 给alertTwo添加第二个输入框
alertTwo.addTextFieldWithConfigurationHandler{ (textfield: UITextField) -> Void in
textfield.placeholder = "第二个输入框"
}
// 创建一个“取消”按钮
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
// 创建一个“确定”按钮
let confirmAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default){ (action: UIAlertAction) -> Void in
// 在此写点击“确定”按钮的触发事件
// ......
print(alertTwo.textFields![0].text!)
print(alertTwo.textFields![1].text!)
}
// 将两个按钮添加到alertTwo
alertTwo.addAction(cancelAction)
alertTwo.addAction(confirmAction)
// 弹出alertTwo
self.presentViewController(alertTwo, animated: true, completion: nil)
3.ActionSheet样式的UIAlertController
Objective-C 版
UIAlertController *alertThree = [UIAlertController alertControllerWithTitle:@"ActionSheet - 1" message:@"这是一个ActionSheet" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
}];
[alertThree addAction:cancelAction];
[alertThree addAction:confirmAction];
[self presentViewController:alertThree animated:YES completion:nil];
Swift 版
let alertThree = UIAlertController.init(title: "ActionSheet - 1", message: "这是一个ActionSheet", preferredStyle: UIAlertControllerStyle.ActionSheet)
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
let confirmAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default){ (action: UIAlertAction) -> Void in
}
alertThree.addAction(cancelAction)
alertThree.addAction(confirmAction)
self.presentViewController(alertThree, animated: true, completion: nil)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)