如何从UIAlertController的textField获取文本/值?

时间:2018-08-11 05:30:33

标签: ios arrays swift textfield uialertcontroller

我正在尝试将在警报视图textField中输入的值提取到数组中,并尝试在控制台中将其打印出来,但是它显示为空,而另一方面,我可以提取viewController的textFields的值并将其打印出来分别。

这是代码 我在UIViewController中全局声明了textField和Array

   var yourArray = [String]()
var textArray = [String]()
var textField = UITextField()

和这个

@objc func createAlert()
{
    let alert = UIAlertController(title: "SAVE DATA", message: "Please type proper name to save this information", preferredStyle: .alert)
    alert.addTextField { (textField) in
        textField.placeholder = "Please enter the name of your relative"

    }
    let action1 = UIAlertAction(title: "SAVE DATA", style: .default) { (action) in




     self.yourArray.append(self.firstName.text!)
        self.yourArray.append(self.lastName.text!)
       self.yourArray.append(self.age.text!)
        self.yourArray.append(self.email.text!)
        self.yourArray.append(self.mobileNumber.text!)

        self.textArray.append(self.textField.text!)


        let a = self.yourArray
        print("The stored array is \(a)")

        let b = self.textArray
        print("TextField alert text value is \(b)")
    }
    alert.addAction(action1)
    present(alert,animated: true, completion: nil)
}

它在控制台中的输出如下:

The stored array is ["James", "Dean", "23", "james@gmail.com", "+12419827865"]

TextField alert text value is " "

我想将textField文本值存储在数组中并在控制台中打印。 感谢您的时间和帮助!

谢谢!

0 个答案:

没有答案