参考文档:https://developer.apple.com/reference/uikit/uialertcontroller
原文:http://hayageek.com/uialertcontroller-example-ios/
UIAlertController 是iOS 8的新特性之一并支持两种样式。 您可以使用它创建一个警告对话框(UIAlerview)或动作表单(UIActionSheet)
typedefNS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet=0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
下面是本文的示例
1) Simple Alert Dialog
2) Alert Dialog with actions
3) Action sheet with actions
4) Create an alert dialog with Textfields
有用的API UIAlertController
下面是UIAlertController的常用的方法。
//Create UIAlertController
+ (instancetype)actionWithTitle:(NSString*)title style:(UIAlertActionStyle)style
handler:(void(^)(UIAlertAction*action))handler;
//Adding an action
- (void)addAction:(UIAlertAction*)action;
//Adding text filed to UIAlertController.This method is supported only for UIAlertControllerStyleAlert
- (void)addTextFieldWithConfigurationHandler:(void(^)(UITextField*textField))configurationHandler;
1) 创建一个简单的Alert对话框
使用以下代码可以创建UIAlertController,使用UIAlertControllerStyleAlert类型。
UIAlertController* alert= [UIAlertController alertControllerWithTitle:@"My Title"
message:@"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController: alert animated: YES completion: nil];
2) 创建一个Alert对话框事件
添加OK /取消按钮到对话框中,您需要创建一个动作并将它添加到UIAlertController。您可以使用下面的代码创建一个操作:
UIAlertAction* ok = [UIAlertAction actionWithTitle: @"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action)
{
//Do some thing here
[view dismissViewControllerAnimated: YES completion: nil];
}];
[alert addAction: ok]; // add action to UIAlertcontroller
下面是UIAlertController的action示例:
UIAlertController* alert= [UIAlertController alertControllerWithTitle:@"Info"
message:@"You are using UIAlertController"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action)
{
[alert dismissViewControllerAnimated: YES completion: nil];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action)
{
[alert dismissViewControllerAnimated: YES completion: nil];
}];
[alert addAction: ok];
[alert addAction: cancel];
[self presentViewController: alert animated: YES completion: nil];
3) 创建一个带Action的动作表单
UIAlertController* view= [UIAlertController alertControllerWithTitle: @"My Title"
message:@"Select you Choice"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action)
{
//Do some thing here
[view dismissViewControllerAnimated: YES completion: nil];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action)
{
[view dismissViewControllerAnimated: YES completion: nil];
}];
[view addAction: ok];
[view addAction: cancel];
[self presentViewController: view animated: YES completion: nil];
4) 创建一个带用户名和密码输入框的AlertView。
添加一个textField可以使用addTextFieldWithConfigurationHandler方法。
添加extField只能是UIAlertControllerStyleAlert类型。
[alert addTextFieldWithConfigurationHandler:^(UITextField*textField) {
textField.placeholder=@"Password";//for passwords
textField.secureTextEntry=YES;
}];
使用以下代码可以创建UIAlertController,使用UIAlertViewStyleLoginAndPasswordInput类型。
UIAlertController* alert= [UIAlertController alertControllerWithTitle:@"My Title"
message:@"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style: UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
//Do Some action here
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style: UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
[alert dismissViewControllerAnimated: YES completion: nil];
}];
[alert addAction: ok];
[alert addAction: cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField*textField) {
textField.placeholder=@"Username";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField*textField) {
textField.placeholder=@"Password";
textField.secureTextEntry=YES;
}];
[self presentViewController: alert animated: YES completion: nil];