參考文檔:https://developer.apple.com/reference/uikit/uialertcontroller
原文:http://hayageek.com/uialertcontroller-example-ios/
UIAlertController 是iOS 8的新特性之一并支持兩種樣式。 您可以使用它創(chuàng)建一個(gè)警告對(duì)話框(UIAlerview)或動(dòng)作表單(UIActionSheet)
typedefNS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet=0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
下面是本文的示例
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) 創(chuàng)建一個(gè)簡(jiǎn)單的Alert對(duì)話框

使用以下代碼可以創(chuàng)建UIAlertController,使用UIAlertControllerStyleAlert類型。
UIAlertController* alert=?? [UIAlertController alertControllerWithTitle:@"My Title"
message:@"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController: alert animated: YES completion: nil];
2) 創(chuàng)建一個(gè)Alert對(duì)話框事件

添加OK /取消按鈕到對(duì)話框中,您需要?jiǎng)?chuàng)建一個(gè)動(dòng)作并將它添加到UIAlertController。您可以使用下面的代碼創(chuàng)建一個(gè)操作:
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) 創(chuàng)建一個(gè)帶Action的動(dòng)作表單

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) 創(chuàng)建一個(gè)帶用戶名和密碼輸入框的AlertView。
添加一個(gè)textField可以使用addTextFieldWithConfigurationHandler方法。
添加extField只能是UIAlertControllerStyleAlert類型。
[alert addTextFieldWithConfigurationHandler:^(UITextField*textField) {
? ?textField.placeholder=@"Password";//for passwords
? ?textField.secureTextEntry=YES;
}];
使用以下代碼可以創(chuàng)建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];