首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertController获取文本

UIAlertController获取文本
EN

Stack Overflow用户
提问于 2014-12-30 01:14:53
回答 1查看 12.8K关注 0票数 8

我正在尝试从UIAlertController文本字段中获取文本。谁能告诉我我做错了什么,因为它不工作。我得到的回报是零。

代码语言:javascript
复制
- (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];

    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-31 03:11:07

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

Swift 3+:

代码语言:javascript
复制
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)

Swift 2:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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];

上一个答案:

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

澄清一下:

UIAlertController实例应该在调用textField之后将textFields保存在它的addTextFieldWithConfigurationHandler数组中。

代码语言:javascript
复制
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.
}

如果由于某种原因不是,请提供更多的信息(因为这个问题仍然受到关注)。它似乎(通过一些评论),一些人对此有一些问题,但没有提供信息,除了‘它不工作’。

原始答案:

您的代码看起来很好,应该可以工作。

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

UITextField *myTf;

addTextFieldWithConfigurationHandler传递textField

代码语言:javascript
复制
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"Enter the name of the recipe";
            textField.keyboardType = UIKeyboardTypeDefault;

            myTf = textField;
        }];

然后,在:

代码语言:javascript
复制
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;

-- 编辑

原始poster的代码现在可以像一样工作(在Xcode7,iOS 9下测试)。可能是以前版本中的错误。可能是因为在以前的版本中,textField是由弱引用持有的,除非另一个强指针持有它,否则会被释放。

票数 36
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27693142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档