【问题标题】:UIAlertController get textUIAlertController 获取文本
【发布时间】:2015-02-25 21:23:52
【问题描述】:

我正在尝试从 UIAlertController 文本字段中获取文本。有人可以告诉我我做错了什么,因为它不起作用。我得到了 NIL 回报。

- (IBAction)btnMakeRec {

        UIAlertController *alert= [UIAlertController
                                   alertControllerWithTitle:@"Recipe Name?"
                                   message:@""
                                   preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action){


                                               UITextField *temp = alert.textFields.firstObject;

                                               RecipeDesc.text = temp.text;
                                               // HERE temp is Nil


                                               RDescription = [[NSString alloc ] initWithFormat:@"%@", RecipeDesc.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 = @"Enter the name of the recipe";
            textField.keyboardType = UIKeyboardTypeDefault;
        }];

        [self presentViewController:alert animated:YES completion:nil];

    }

}

【问题讨论】:

  • 尝试在同一块中插入NSLog(@"alert.textFields: %@", alert.textFields);
  • 好的,我做了 NSLog 并得到了这个:alert.textFields: ("<_uialertcontrollertextfield: frame="(4" text="yyyy" clipstobounds="YES;" opaque="NO;gestureRecognizers">; layer = >" ) 查看我输入的 text="yyyy"。那么如何将该文本放入我的字符串中呢?

标签: ios8 xcode6 uialertcontroller


【解决方案1】:

这是“干净的复制/粘贴”版本:

Swift 3+:

let alert = UIAlertController(title: "Alert Title",
                              message: "Alert message",
                              preferredStyle: .alert)

let ok = UIAlertAction(title: "OK",
                       style: .default) { (action: UIAlertAction) in
                        
                        if let text = alert.textFields?.first?.text {
                            
                            print("And the text is... \(text)")
                            
                        }
                        
                        
}

let cancel = UIAlertAction(title: "Cancel",
                           style: .cancel,
                           handler: nil)

alert.addTextField { (textField: UITextField) in
    
    textField.placeholder = "Text here"
    
}

alert.addAction(ok)
alert.addAction(cancel)

self.present(alert, animated: true, completion: nil)

斯威夫特 2:

let alert = UIAlertController(title: "Alert Title",
                              message: "Alert message",
                              preferredStyle: UIAlertControllerStyle.Alert)

let ok = UIAlertAction(title: "OK",
                       style: UIAlertActionStyle.Default) { (action: UIAlertAction) in
                        
                        if let alertTextField = alert.textFields?.first where alertTextField.text != nil {
                            
                            print("And the text is... \(alertTextField.text!)!")
                            
                        }
                        
                        
}

let cancel = UIAlertAction(title: "Cancel",
                           style: UIAlertActionStyle.Cancel,
                           handler: nil)

alert.addTextFieldWithConfigurationHandler { (textField: UITextField) in
    
    textField.placeholder = "Text here"
    
}

alert.addAction(ok)
alert.addAction(cancel)

self.presentViewController(alert, animated: true, completion: nil)

目标 C:

UIAlertController *alert = [UIAlertController
                           alertControllerWithTitle: @"Alert Title"
                           message: @"Alert message"
                           preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *ok = [UIAlertAction actionWithTitle: @"OK" style: UIAlertActionStyleDefault
                                           handler:^(UIAlertAction *action){
                                               
                                               
                                               UITextField *alertTextField = alert.textFields.firstObject;
                                               
                                               NSLog(@"And the text is... %@!", alertTextField.text);
                                               
                                           }];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                               handler: nil];


[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

    textField.placeholder = @"Text here";

}];

[alert addAction:ok];
[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];

上一个答案:

编辑:- 请注意:目前,原始问题似乎按原样工作。

澄清一下:

在调用addTextFieldWithConfigurationHandler 之后,UIAlertController 实例应将 textField 保存在其 textFields 数组中。

UIAlertController *alertController = ...// Create alert

// Assuming you called 'addTextFieldWithConfigurationHandler' on 'alertController'

UIAlertAction *action = [UIAlertAction actionWithTitle: ... handler:^(UIAlertAction * action) {
    
    // alertController.textFields should hold the alert's text fields.
}

如果由于某种原因不是,请提供更多说明(因为此问题仍在引起关注)。似乎(一些 cmets)有些人对此有一些问题,但除了“它不起作用”之外没有提供信息。


原答案:

您的代码看起来不错,应该可以工作。

另一种方法是在UIAlertController *alert=...之前定义一个UITextField

UITextField *myTf;

addTextFieldWithConfigurationHandler传递textField:

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"Enter the name of the recipe";
            textField.keyboardType = UIKeyboardTypeDefault;

            myTf = textField;
        }];

然后,在:

UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action){...
//UITextField *temp = alert.textFields.firstObject;

// Get the text from your textField
NSString *temp = myTf.text;

-- 编辑

原始海报的代码现在可以按原样运行(在 Xcode 7、iOS 9 下测试)。可能是以前版本中的错误。可能是在以前的版本中,textField 被一个弱引用持有并被释放,除非另一个强指针持有它。

【讨论】:

  • 行得通。仍然不确定为什么它不能以另一种方式工作。感谢您的帮助!
  • 在屏幕上显示此警报后,它不会按您的方式工作,因为您会丢失对“警报”的引用。您应该在 ViewController 的全局变量中保留一个引用。
  • @HoaParis 首先,它确实有效。在一个干净的项目上试一试。目前它的工作原理与 OP 完全一样(以前版本中的错误?)。其次,关于块的有趣之处在于,它们捕获变量。
  • 它不起作用(iOS9)。 myTf.text 不返回字段的更新文本。
  • @Fabrizio 我只是将这段代码复制/粘贴到一个新项目中,它在这里工作。请看看你是否在你的项目中做一些不同的事情。 “更新文本”是什么意思?听起来有点不同的问题。并感谢您击倒积分。
猜你喜欢
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多