alertcontroller

對話框

一般來說,根據(jù)蘋果官方制定的《iOS 用戶界面指南》,在擁有兩個按鈕的對話框中,您應(yīng)當(dāng)將取消按鈕放在左邊

要注意,取消按鈕是唯一的,如果您添加了第二個取消按鈕,那么你就會得到如下的一個運行時異常:

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘UIAlertController can only have one action with a style of UIAlertActionStyleCancel’

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標(biāo)題" message:@"這個是UIAlertController的默認(rèn)樣式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

警示

根據(jù)蘋果官方的定義,“警示”樣式的按鈕是用在可能會改變或刪除數(shù)據(jù)的操作上。因此用了紅色的醒目標(biāo)識來警示用戶。

UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:resetAction];

內(nèi)置文本框

我們可以向?qū)υ捒蛑刑砑尤我鈹?shù)目的UITextField對象,并且可以使用所有的UITextField特性。當(dāng)您向?qū)υ捒蚩刂破髦刑砑游谋究驎r,您需要指定一個用來配置文本框的代碼塊

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本對話框" message:@"登錄和密碼對話框示例" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
    textField.placeholder = @"登錄";
}];
 [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"密碼";
    textField.secureTextEntry = YES;
}];

讀取文本框中的值

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    UITextField *login = alertController.textFields.firstObject;
    UITextField *password = alertController.textFields.lastObject;
    ...
}];

假定我們要讓“登錄”文本框中至少有3個字符才能激活“好的”按鈕。

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
    ...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];

當(dāng)視圖控制器釋放的時候我們需要移除這個Observer,我們通過在每個按鈕動作的handler代碼塊(還有其他任何可能釋放視圖控制器的地方)中添加合適的代碼來實現(xiàn)它。比如說在okAction這個按鈕動作中:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    ...
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}];

在顯示對話框之前,我們要凍結(jié)“好的”按鈕

okAction.enabled = NO;

接下來,在通知觀察者(notification observer)中,我們需要在激活按鈕狀態(tài)前檢查“登錄”文本框的內(nèi)容。

- (void)alertTextFieldDidChange:(NSNotification *)notification{
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if (alertController) {
        UITextField *login = alertController.textFields.firstObject;
        UIAlertAction *okAction = alertController.actions.lastObject;
        okAction.enabled = login.text.length > 2;
    }
}

上拉菜單

就是從底部升上來的那種

  • 注意, iPhone和ipad樣式代碼不同*
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存或刪除數(shù)據(jù)" message:@"刪除數(shù)據(jù)將不可恢復(fù)" preferredStyle: UIAlertControllerStyleActionSheet];

添加按鈕動作的方式和對話框相同。

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAction];

《iOS 用戶界面指南》要求所有的“毀壞”樣式按鈕都必須排名第一

展示

[self presentViewController:alertController animated:YES completion:nil];

對話窗口帶尖角, 在常規(guī)寬度的設(shè)備上(非緊縮型, 應(yīng)該是指ipad吧),上拉菜單是以彈出框的形式展現(xiàn)。彈出框必須要有一個能夠作為源視圖或者欄按鈕項目的描點(anchor point)。由于在本例中我們是使用了常規(guī)的UIButton來觸發(fā)上拉菜單的,因此我們就將其作為描點

UIPopoverPresentationController *popover = alertController.popoverPresentationController;
if (popover){
    popover.sourceView = sender;
    popover.sourceRect = sender.bounds;
    popover.permittedArrowDirections =   UIPopoverArrowDirectionAny;
}

通常情況下,當(dāng)用戶選中一個動作后對話框控制器將會自行釋放。不過您仍然可以在需要的時候以編程方式釋放它,就像釋放其他視圖控制器一樣。您應(yīng)當(dāng)在應(yīng)用程序轉(zhuǎn)至后臺運行時移除對話框或者上拉菜單。假定我們正在監(jiān)聽UIApplicationDidEnterBackgroundNotification通知消息,我們可以在observer中釋放任何顯示出來的視圖控制器。(參考在viewDidLoad方法中設(shè)立observer的示例代碼)。要保證運行安全我們同樣要確保移除所有的文本框observer。

- (void)didEnterBackground:(NSNotification *)notification
{
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
  [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
}

感謝http://www.cocoachina.com/ios/20141126/10320.html, 此文也有所有代碼的swift版本

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

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

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