How to add text input in alertview of ios 8?

前端 未结 9 2382
眼角桃花
眼角桃花 2020-12-07 16:46

I want to add text input in alert-view of ios 8. I know it was done using UIAlertController but not have any idea. How to do it ?

相关标签:
9条回答
  • 2020-12-07 17:07
    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];
    

    you can cover this way .Thanks

    0 讨论(0)
  • 2020-12-07 17:08

    Here's handy method with submit/cancel and completion handler for text if inputted:

    /**
     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)
    }
    
    0 讨论(0)
  • 2020-12-07 17:09

    Example of implementation with 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)
    
    0 讨论(0)
  • 2020-12-07 17:10
    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];
    

    also, you will need to implement UITextFieldDelegate, UIAlertViewDelegate protocols.

    and if you user uialertcontroller then use this one

    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];
    
    0 讨论(0)
  • 2020-12-07 17:18

    UIAlertViewController with text input

    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];
    
    0 讨论(0)
  • 2020-12-07 17:20

    This one is work for me:

    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)
    
    0 讨论(0)
提交回复
热议问题