5

alertViewStyleに設定して UIAlertView を作成しましたUIAlertViewStylePlainTextInput

キーボードの種類を変更したいのですが、サブビューを追加することはできません。

マイコード:</p>

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"a" message:@"b" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"aaa", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;  
[alert show];
4

1 に答える 1

44

入力スタイルを表示する場合は、まずクラスにUIAlertViewDelegateを実装します。

次に、alertView を提示するときに、デリゲートを自分自身に設定します。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"a" message:@"b" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"aaa", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;  
[alert show];'

特定のフィールドのキーボード タイプを変更する場合は、次のようにします。

たとえば、最初のフィールドの場合、キーボードは数字になります。

[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alert textFieldAtIndex:0] becomeFirstResponder];

iOS 8.0 Swift の更新

iOS 8.0 以降 UIAlertViewは非推奨なので、使用する必要があるかもしれませんUIAlertController

 var alert = UIAlertController(title: "Title", message: "Your msg", 
                preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, 
              handler:yourHandler))

        alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
            textField.placeholder = "Password"
            textField.secureTextEntry = true  // setting the secured text for using password
            textField.keyboardType = UIKeyboardType.Default
        })
于 2013-04-02T09:49:26.823 に答える