How to add text input in alertview of ios 8?

前端 未结 9 2383
眼角桃花
眼角桃花 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:23

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

    }

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

    Screenshot

    Code

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