iOS UIAlertController 的使用

參考文檔: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);

下面是本文的示例

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) 創(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];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容