2015-12-29 9 views
25

enter image description hereUIAlertControllerでtextFieldを追加するには?

enter image description here

私はパスワードの変更に関する機能を実現したい、それが入力に彼の前のパスワードをユーザーが必要です、と私は警告ダイアログでそれを設計し、私は「ボタンをクリックします変更を確認"その後、パスワードを変更するために他のView Controllerにジャンプします。私はいくつかのコードを書いていますが、次の瞬間に書き込む方法はわかりません。

+0

https://github.com/KiritVaghela/UIAlertController –

答えて

22

あなたは、コントローラに警告し、新しいパスワードが空の文字列である場合は、警告コントローラのテキストフィールドプロパティ

でそれらにアクセスし、再び警告を提示するために、複数のテキストフィールドを追加することができます。それとも別の方法...最初の無効確認ボタンは、テキストフィールドがテキスト

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"confirm the modification" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { 
    UITextField *password = alertController.textFields.firstObject; 
    if (![password.text isEqualToString:@""]) { 

     //change password 

    } 
    else{ 
     [self presentViewController:alertController animated:YES completion:nil]; 
    } 
}]; 
+0

yはalertcontroller.Soのテキストフィールドを取得する方法です私はこの行が非常に重要だと思います! UITextField * password = alertController.textFields.firstObject; 私はこのコードを知っていて、次のコードを書くことができます。 – Juice007

+0

確認ボタンを無効にする方法 – iOSGeek

+0

アクションのブロック内で 'alertController'を参照すると、保持サイクルになります。 '__weak typeof(alertController)weakAlert = alertController;' ... 'UIAlertAction * ...' '... password = weakAlert.textFields.firstObject;' – beebcon

39

を持っている場合にのみ、あなたは、そのtextFields読み取り専用プロパティで、警告コントローラから追加されたすべてのテキストフィールドを取得することを可能にするには、そのテキストを取得するためにそれを使用することができます。

同様スウィフト3:

let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert) 
alertController.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in 
    textField.placeholder = "Current password" 
    textField.isSecureTextEntry = true 
}) 
let confirmAction = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in 
    print("Current password \(String(describing: alertController.textFields?[0].text))") 
    //compare the current password and do action here 
}) 
alertController.addAction(confirmAction) 
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(_ action: UIAlertAction) -> Void in 
    print("Canelled") 
}) 
alertController.addAction(cancelAction) 
present(alertController, animated: true, completion: { _ in }) 

注:

: alertController.textFields [0]オプションであるの.text、

のObjective-Cを使用する前にアンラップ

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"Current password"; textField.secureTextEntry = YES; }]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"Current password %@", [[alertController textFields][0] text]); //compare the current password and do action here }]; [alertController addAction:confirmAction]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"Canelled"); }]; [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil]; 

[[alertController textFields][0] text]この行は、最初のテキストフィールドをalerControllerに追加し、そのテキストを取得します。テキストを受け入れながら enter image description here

そして:

// Create a standard UIAlertController 
let alertController = UIAlertController(title: "Password Entry", message: "", preferredStyle: .alert) 

// Add a textField to your controller, with a placeholder value & secure entry enabled 
alertController.addTextField { textField in 
    textField.placeholder = "Enter password" 
    textField.isSecureTextEntry = true 
    textField.textAlignment = .center 
} 

// A cancel action 
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in 
    print("Canelled") 
} 

// This action handles your confirmation action 
let confirmAction = UIAlertAction(title: "OK", style: .default) { _ in 
    print("Current password value: \(alertController.textFields?.first?.text ?? "None")") 
} 

// Add the actions, the order here does not matter 
alertController.addAction(cancelAction) 
alertController.addAction(confirmAction) 

// Present to user 
present(alertController, animated: true, completion: nil) 

そして、最初に提示するとき、それがどのように見えるか:ここで

+0

私は何を知りたいのですか?このコードは "[alertController textFields] [0] text"という意味ですか? – Juice007

+1

@ Juice007これは、alertcontrollerの最初のテキストフィールドを取得し、テキストを取得していることを意味します。 –

+1

あなたは保持サイクルを避けるために '[weak alertController]'を使うべきです。 – dOM

7

は、テキストフィールドの必要な種類を作成しスウィフト4.0の更新された答えです

enter image description here

関連する問題