【问题标题】:How to add text input in alertview of ios 8?如何在 ios 8 的 alertview 中添加文本输入?
【发布时间】:2016-03-03 23:04:27
【问题描述】:

我想在 ios 8 的警报视图中添加文本输入。 我知道它是使用UIAlertController 完成的,但不知道。 怎么做 ?

【问题讨论】:

  • addTextFieldWithConfigurationHandler:?

标签: ios objective-c uialertcontroller


【解决方案1】:

这个对我有用:

let passwordString = lableGetPassword.text
    var textField: UITextField?
    
    // create alertController
    let alertController = UIAlertController(title: "Password", message: "Save the password. Give a tag name.", preferredStyle: .alert)
    alertController.addTextField { (pTextField) in
        pTextField.placeholder = "Tag Name"
        pTextField.clearButtonMode = .whileEditing
        pTextField.borderStyle = .none
        textField = pTextField
    }
    
    // create cancel button
    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (pAction) in
        alertController.dismiss(animated: true, completion: nil)
    }))
    
    // create Ok button
    alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { [self] (pAction) in
        // when user taps OK, you get your value here
        let name = textField?.text
        save(name: name!, password: passwordString!)
        alertController.dismiss(animated: true, completion: nil)
    }))
    
    // show alert controller
    self.present(alertController, animated: true, completion: nil)

【讨论】:

    【解决方案2】:
      func askForName() {
    let alert = UIAlertController(title: "Enter Text",
                                  message: "Enter some text below",
                                  preferredStyle: .alert)
    
    alert.addTextField { (textField) in
      textField.text = "New Text"
    }
    
    let action = UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
      let textField = alert!.textFields![0]
      print("Text field: \(textField.text)")
    })
    
    alert.addAction(action)
    
    present(alert, animated: true, completion: nil)
    

    }

    【讨论】:

      【解决方案3】:

      这里有一个方便的方法,带有提交/取消和文本完成处理程序(如果输入):

      /**
       Presents an alert controller with a single text field for user input
      
       - parameters:
          - title: Alert title
          - message: Alert message
          - placeholder: Placeholder for textfield
          - completion: Returned user input
       */
      
      func showSubmitTextFieldAlert(title: String,
                                    message: String,
                                    placeholder: String,
                                    completion: @escaping (_ userInput: String?) -> Void) {
      
          let alertController = UIAlertController(title: title,
                                                  message: message,
                                                  preferredStyle: .alert)
      
          alertController.addTextField { (textField) in
              textField.placeholder = placeholder
              textField.clearButtonMode = .whileEditing
          }
      
          let submitAction = UIAlertAction(title: "Submit", style: .default) { (action) in
              let userInput = alertController.textFields?.first?.text
              completion(userInput)
          }
      
          let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in
              completion(nil)
          }
      
          alertController.addAction(submitAction)
          alertController.addAction(cancelAction)
      
          present(alertController, animated: true)
      }
      

      【讨论】:

        【解决方案4】:

        使用 Swift 3 实现的示例:

        var textField: UITextField?
        
        // create alertController
        let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
          alertController.addTextField { (pTextField) in
          pTextField.placeholder = "usefull placeholdr"
          pTextField.clearButtonMode = .whileEditing
          pTextField.borderStyle = .none
          textField = pTextField
        }
        
        // create cancel button
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (pAction) in
          alertController.dismiss(animated: true, completion: nil)
        }))
        
        // create Ok button
        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (pAction) in
          // when user taps OK, you get your value here
          let inputValue = textField?.text
          alertController.dismiss(animated: true, completion: nil)
        }))
        
        // show alert controller
        self.present(alertController, animated: true, completion: nil)
        

        【讨论】:

          【解决方案5】:
          UIAlertView *myView = [[UIAlertView alloc]initWithTitle:@"Input" message:@"Enter your value" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
          myView.alertViewStyle = UIAlertViewStylePlainTextInput;
          [myView textFieldAtIndex:0].delegate = self;
          [myView show];
          

          你可以这样覆盖。谢谢

          【讨论】:

            【解决方案6】:

            带有文本输入的 UIAlertViewController

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title"
                                                                           message:nil
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                // optionally configure the text field
                textField.keyboardType = UIKeyboardTypeAlphabet;
            }];
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                                               style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction *action) {
                                                                 UITextField *textField = [alert.textFields firstObject];
                                                                 textField.placeholder = @"Enter Input";
                                                             }];
            [alert addAction:okAction];
            
            [self presentViewController:alert animated:YES completion:nil];
            

            【讨论】:

              【解决方案7】:
              UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Please enter someth" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
              
              av.alertViewStyle = UIAlertViewStylePlainTextInput;
              
              [av textFieldAtIndex:0].delegate = self;
              [av show];
              

              此外,您还需要实现 UITextFieldDelegate、UIAlertViewDelegate 协议。

              如果您使用 uialertcontroller,则使用这个

              UIAlertController * alert=   [UIAlertController
                                            alertControllerWithTitle:@"My Title"
                                            message:@"Enter User Credentials"
                                            preferredStyle:UIAlertControllerStyleAlert];
              
              UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
                                                             //Do Some action here
              
                                                         }];
              UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 [alert dismissViewControllerAnimated:YES completion:nil];
                                                             }];
              
              [alert addAction:ok];
              [alert addAction:cancel];
              
              [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                  textField.placeholder = @"Username";
              }];
              [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                  textField.placeholder = @"Password";
                  textField.secureTextEntry = YES;
              }];
              
              [self presentViewController:alert animated:YES completion:nil];
              

              【讨论】:

                【解决方案8】:

                AlertViewController

                        // use UIAlertController
                        UIAlertController *alert= [UIAlertController
                                                      alertControllerWithTitle:@"Title"
                                                      message:@"SubTitle"
                                                      preferredStyle:UIAlertControllerStyleAlert];
                
                        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                   handler:^(UIAlertAction * action){
                                                                       //Do Some action here
                                                                       UITextField *textField = alert.textFields[0];
                                                                       NSLog(@"text was %@", textField.text);
                
                                                                   }];
                        UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                                       handler:^(UIAlertAction * action) {
                
                                                                           NSLog(@"cancel btn");
                
                                                                           [alert dismissViewControllerAnimated:YES completion:nil];
                
                                                                       }];
                
                        [alert addAction:ok];
                        [alert addAction:cancel];
                
                        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                            textField.placeholder = @"placeHolderText";
                            textField.keyboardType = UIKeyboardTypeDefault;
                        }];
                
                        [self presentViewController:alert animated:YES completion:nil];
                

                UIAlertView

                        UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Title"
                                                                         message:@"SubTitle"
                                                                        delegate:self
                                                               cancelButtonTitle:@"Cancel"
                                                               otherButtonTitles:@"OK", nil];
                
                        dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
                
                        [dialog show];
                
                    }
                

                【讨论】:

                  【解决方案9】:

                  截图

                  代码

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

                  【讨论】:

                  • 您不能在操作块中引用 alertController.textFields。创建循环引用。改为创建 __block 变量。
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-12-16
                  • 2014-07-24
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2010-12-21
                  • 1970-01-01
                  相关资源
                  最近更新 更多