无法在AlertController中添加文本字段

时间:2017-08-09 08:56:20

标签: ios iphone swift3 uitextfield uialertcontroller

var textFD = UITextField.init()
var tempStr : String!
var datax = [Tasks]()
var tempArr = [NSManagedObject]()
override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "Add", style: UIBarButtonItemStyle.plain, target: self, action:#selector(myTableViewController.addFunc))
    print("Hello")
     fetchData()
}
 func addFunc(){
    let alertO = UIAlertController.init(title: "Add", message: "Add what you want", preferredStyle: .alert)
    let saveAct=UIAlertAction.init(title: "Save", style: .default, handler:saveHandler)
    let cancelAct=UIAlertAction.init(title: "Cancel", style: .default, handler: nil)
    alertO.addAction(saveAct)
    alertO.addAction(cancelAct)
    alertO.addTextField(configurationHandler: { (textFD) in

        textFD.placeholder="Hey therer"

        print("textfd text1 is \n\n\n" ,textFD.text!)
        self.tempStr=textFD.text!
    })
    print("textfd text2 is \n\n\n",textFD.text! )
    self.present(alertO, animated: true, completion: nil)
    self.tableView.reloadData()
    self.tempStr=textFD.text!

}
func saveHandler(alert:UIAlertAction){
    print("textfd text3 is \n\n\n",textFD.text! )
    print("Value of tempStr \n\n ",tempStr)
    let taskX = Tasks(context: context)
    taskX.mydata = tempStr
    appDelegate.saveContext()
    self.tableView.reloadData()
    print("Value of tempStr \n\n ",tempStr)

}

当我点击按钮时,addFunc将调用警报,并通过警报控制器上的文本字段,稍后文本字段的文本将存储在字符串中。

当我尝试将文本字段文本存储到字符串中时,会出现Alertcontroller,文本字段超过警报和文本字段占位符(tempStr) 它无法存储!

3 个答案:

答案 0 :(得分:0)

试试这个

修改

func addFunc(){
    let alert = UIAlertController(title: "MESSAGE".localized, message: "ENTER_YOUR_MESSAGE".localized, preferredStyle: .alert)

            alert.addTextField { (textField) in
                // textField.placeholder = "Enter your message".localized
            }

            alert.addAction(UIAlertAction(title: "TITLE".localized, style: .default, handler: { (action:UIAlertAction!) in
                let textField = alert.textFields![0] // Force unwrapping because we know it exists.
                //print("Text field: \(String(describing: textField?.text))")

                if (textField.text?.characters.count)! > 0
                {
                    self.tempStr = textField.text

                }
                else
                {
                    print("empty text")
                }
            }))
            self.present(alert, animated: true, completion: nil)
}

答案 1 :(得分:0)

此类的属性textFD不是alertO上显示的属性,textField的configurationHandler只提供一个闭包来帮助用户在UIAlertController上自定义textField。

要在UIAlertController上获取textField的文本,首先需要使用UIAlertController实例的属性textFields来获取所需的textFiled,然后使用.text获取此textFiled的文本。

答案 2 :(得分:0)

alertO.addTextField(configurationHandler: { (textFD) in

    textFD.placeholder="Hey therer"

    print("textfd text1 is \n\n\n" ,textFD.text!)
    self.tempStr=textFD.text!
})

textfield正在初始化,没有文本(默认为空白),tempStr正在存储空白值。你必须在保存动作中保存字符串。

      let saveAct = UIAlertAction.init(title: "Save", style: .default, handler:{
            alertAction in
            let textField = alertO.textFields![0] as UITextField
            self.tempStr = textField.text
     })