【问题标题】:Display an Alert with Text Field Entry [duplicate]显示带有文本字段条目的警报 [重复]
【发布时间】:2016-07-21 14:40:29
【问题描述】:

如何在警报中显示文本输入字段以获取用户输入并在应用程序中使用该输入(在标签上显示输入)?

如下图

感谢您的宝贵时间。

【问题讨论】:

    标签: ios objective-c alert


    【解决方案1】:
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Text"
                                                                    message:@"Enter some text below"
                                                             preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *submit = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
    
                                                       if (alert.textFields.count > 0) {
    
                                                           UITextField *textField = [alert.textFields firstObject];
    
                                                           textField.text // your text
                                                       }
    
                                                   }];
    
    [alert addAction:submit];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"something"; // if needs
    }];
    
    [self presentViewController:alert animated:YES completion:nil];
    

    【讨论】:

      【解决方案2】:

      要将 TextField 添加到 UIAlertViewalertViewStyle 属性设置为 UIAlertViewStylePlainTextInput

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
                                                      message:@"Message"
                                                     delegate:self
                                            cancelButtonTitle:@"Done"
                                            otherButtonTitles:nil];
      alert.alertViewStyle = UIAlertViewStylePlainTextInput;
      [alert show];
      

      在 .h 文件中添加 UIAlertViewDelegate 作为协议,并在 .m 文件中实现委托方法alertView:clickedButtonAtIndex

      -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
          NSLog(@"%@", [alertView textFieldAtIndex:0].text);
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-11
        • 1970-01-01
        • 1970-01-01
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多