[iOS] Dialog Box

Posted in :

iOS 的輸入框,是使用 UIAlertController。

 

一個簡單的對話框例子

您可以比較一下兩種不同的創建對話框的代碼,創建基礎UIAlertController的代碼和創建UIAlertView的代碼非常相似:

Objective-C版本:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題" message:@"這個是UIAlertController的默認樣式" preferredStyle:UIAlertControllerStyleAlert];

swift版本:

var alertController = UIAlertController(title: "標題", message: "這個是UIAlertController的默認樣式", preferredStyle: UIAlertControllerStyle.Alert)

同創建UIAlertView相比,我們無需指定代理,也無需在初始化過程中指定按鈕。不過要特別注意第三個參數,要確定您選擇的是對話框樣式還是上拉菜單樣式。

 


How to add text input in alertview of ios 8?
http://stackoverflow.com/questions/33996443/how-to-add-text-input-in-alertview-of-ios-8

 

 UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Login"
                                                                                  message: @"Input username and password"
                                                                              preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"name";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"password";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.secureTextEntry = YES;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray * textfields = alertController.textFields;
        UITextField * namefield = textfields[0];
        UITextField * passwordfiled = textfields[1];
        NSLog(@"%@:%@",namefield.text,passwordfiled.text);

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

 

 


看要增加幾個選項按鈕都可以,而且都是用Block來做後續處理

範例:

 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];

UIAlertController Example in iOS
http://hayageek.com/uialertcontroller-example-ios/

UIAlertController Changes in iOS 8
https://useyourloaf.com/blog/uialertcontroller-changes-in-ios-8/

 

 

Swift 範例:
https://www.simplifiedios.net/ios-dialog-box-with-input/

 

Apple 官方文件:

UIAlertController
https://developer.apple.com/reference/uikit/uialertcontroller

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *