热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UIAlertController获取文本

如何解决《UIAlertController获取文本》经验,为你挑选了1个好方法。

我试图从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];

    }

}

bauerMusic.. 30

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

Swift 3&4:

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, alertTextField.text != nil {

                            print("And the text is... \(alertTextField.text!)!")

                        }


}

let cancel = UIAlertAction(title: "Cancel",
                           style: UIAlertActionStyle.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];

上一个答案:

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

澄清:

UIAlertController调用后实例应该持有它的文本框数组文本字段addTextFieldWithConfigurationHandler.

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.
}

如果由于某种原因它没有,请放弃更多的光(因为这个问题仍然受到关注).似乎(通过一些评论)有些人对此有一些问题,但没有提供超出"它不起作用"的信息.


原始答案:

你的代码看起来很好,它应该工作.

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

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由弱引用持有并被释放,除非另一个强指针持有它.



1> bauerMusic..:

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

Swift 3&4:

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, alertTextField.text != nil {

                            print("And the text is... \(alertTextField.text!)!")

                        }


}

let cancel = UIAlertAction(title: "Cancel",
                           style: UIAlertActionStyle.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];

上一个答案:

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

澄清:

UIAlertController调用后实例应该持有它的文本框数组文本字段addTextFieldWithConfigurationHandler.

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.
}

如果由于某种原因它没有,请放弃更多的光(因为这个问题仍然受到关注).似乎(通过一些评论)有些人对此有一些问题,但没有提供超出"它不起作用"的信息.


原始答案:

你的代码看起来很好,它应该工作.

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

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由弱引用持有并被释放,除非另一个强指针持有它.


推荐阅读
author-avatar
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有