Just My Life & My Work

[iOS] UIAlertController

UIAlertView已經被棄用,iOS 8開始由UIAlertController取代它囉!如此變得更親切迷人~

iOS UIAlertController.jpg

過去能透過UIAlertView做出可填寫欄位UITextField,現在UIAlertController也能實現,而且更加方便好操作。

例如上圖程式碼這麼寫:

/**
 Theme: UIAlertController
 IDE: Xcode 7
 Language: Objective C
 Date: 105/03/30
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改暱稱"
                                                                   message:@""
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // optionally configure the text field
        textField.keyboardType = UIKeyboardTypeDefault;
    }];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"送出"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action) {
                                                         UITextField *textField = [alert.textFields firstObject];

                                                     }];
    [alert addAction:okAction];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alert addAction:cancelAction];

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

看要增加幾個選項按鈕都可以,而且都是用Block來做後續處理,不用再像過去還要跳來跳去的Delegate

參考:About UIAlertView with Textfield

廣告

隨意留個言吧:)~

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

標籤雲

%d 位部落客按了讚: