UIAlertView + TextField =错误?

时间:2015-06-07 12:38:35

标签: ios swift uialertview

我正在创建一个应用程序,因此可以将数据保存到Table View中,我希望以某种方式使用UIAlertView Controller,以便人们可以使用它创建一个新的Cell(任务),但我知道如何创建一个textField一个UIAlertView,但如何使用textField数据并在UIAlertView Action按钮中使用它!

@IBAction func addNewTask() {

        var alertController = UIAlertController(title: "Add A New Task", message: "Add The Correct Information To Add A New Task!", preferredStyle: .Alert)

        // Create the actions
        var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
            UIAlertAction in
            NSLog("OK Pressed")


        }
        var cancelAction = UIAlertAction(title: "Exit App", style: UIAlertActionStyle.Cancel) {
            UIAlertAction in
            NSLog("User Exited The App!")
            exit(2)

        }

        alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
            // Here you can configure the text field (eg: make it secure, add a placeholder, etc)

        }

        // Add the actions
        alertController.addAction(Exit)

        alertController.addAction(cancelAction)

        // Present the controller
        self.presentViewController(alertController, animated: true, completion: nil)



    }

这是代码,但我如何使用alertController.addTextFieldWithConfigureationHandler中的数据并将其放入Exit操作中!所以我想要做的主要事情只是知道当用户键入文本框并单击退出时,标签将从textField更改为该文本但不使用viewController!我正在使用Swift,Xcode 6.3.1

谢谢,

George Barlow

P.S。对不起那个冗长的问题!

3 个答案:

答案 0 :(得分:1)

在名为UIAlertAction的{​​{1}}中(顺便说一句,您应该考虑将名称更改为Exitexit),您可以访问exitAction及其内容如下:

UITextField

答案 1 :(得分:0)

“... ..您还可以向警报界面添加文本字段。警报控制器允许您在显示之前提供用于配置文本字段的块。警报控制器维护对每个文本字段的引用,以便您可以访问它的价值以后。“ https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html#//apple_ref/occ/cl/UIAlertController

您可以访问UIAlertViewController上的textfield属性。

alertController.textFields

文本字段将按照您将它们添加到警报控制器的顺序添加到此数组中。

在完成退出操作的内部:

UITextField *taskTextField = alertController.textFields.firstObject

*编辑:在swift:

if let taskTextField = alertController.textFields![0] as! UITextField {
    let taskText = taskTextField.text
    ...
}

答案 2 :(得分:0)

您可以这样做:

class ViewController: UIViewController {

        @IBOutlet weak var myLabel: UILabel!
        var mytextField: UITextField!

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

        }

        override func viewDidAppear(animated: Bool) {
            addNewTask()
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


        func addNewTask() {
            var alertController = UIAlertController(title: "Add A New Task", message: "Add The Correct Information To Add A New Task!", preferredStyle: .Alert)

            // Create the actions
            var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
                UIAlertAction in
                NSLog("OK Pressed")

                self.myLabel.text = self.mytextField.text


            }
            var cancelAction = UIAlertAction(title: "Exit App", style: UIAlertActionStyle.Cancel) {
                UIAlertAction in
                NSLog("User Exited The App!")
                exit(2)

            }

            alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
                // Here you can configure the text field (eg: make it secure, add a placeholder, etc)

                self.mytextField = textField
            }

            // Add the actions
            alertController.addAction(Exit)

            alertController.addAction(cancelAction)

            // Present the controller
            self.presentViewController(alertController, animated: true, completion: nil)
        }


    }