如何从警报文本字段中的用户输入中获取字符串

时间:2017-12-27 10:23:15

标签: ios swift

我试图从这样的警报中获取用户输入文本字段中的字符串,但我总是得到零值。我用来表示警报文本字段的代码是这样的

 @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
        // show an alert to show up a quick textfield


        var textField = UITextField()
        let itemTitle = textField.text!

        print("the title is \(itemTitle) ....")


        let alert = UIAlertController(title: "Add Item to your to do list", message: "", preferredStyle: .alert)
        let addItemAction = UIAlertAction(title: "Add item", style: .default) { (action) in
            // after user click "add item", the new item will be displayed to table view

            let newItem = Item()
            newItem.title = itemTitle


            self.itemArray.append(newItem)
            self.saveItems()


        }


        alert.addTextField { (alertTextfield) in
            alertTextfield.placeholder = "Create a new item"
            textField = alertTextfield
            // store a data from user to 'textField' variable, so it can be accessed in closure at 'addItemAction'
        }



        alert.addAction(addItemAction)
        present(alert, animated: true, completion: nil)

    }

我的用户界面

enter image description here

我正在尝试打印itemTitle,但我从来没有从用户那里得到字符串。像这样

输出

enter image description here

这里出了什么问题?

4 个答案:

答案 0 :(得分:2)

一旦用户按下Add item之前,您将尝试打印它。

检查以下代码:

let addItemAction = UIAlertAction(title: "Add item", style: .default) { (action) in

    // after user click "add item", the new item will be displayed to table view
    print(textField.text)  //Here you will get user text.

    let newItem = Item()
    newItem.title = textField.text //add it here
    self.itemArray.append(newItem)
    self.saveItems()   
}

答案 1 :(得分:2)

按下Add Item时,您必须从textField获取文本。

更改此

newItem.title = itemTitle

newItem.title = textField.text

答案 2 :(得分:2)

试试这个

   /// define alert controller 
  let alert = UIAlertController(title: "Alert Title",
                          message: "Alert message",
                          preferredStyle: UIAlertControllerStyle.alert)

 /// add OK Action
  let ok = UIAlertAction(title: "OK",
                   style: UIAlertActionStyle.default) { (action: UIAlertAction) in

    /// get the textfield instance form textFields array

                    if let alertTextField = alert.textFields?.first, alertTextField.text != nil {

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

                    }


  }

     /// add CANCEL Action

   let cancel = UIAlertAction(title: "Cancel",
                       style: UIAlertActionStyle.cancel,
                       handler: nil)

    /// add first textfeild to the alertController
    alert.addTextField { (textField: UITextField) in

    /// configure textfeild with properties like placeholder and textColor
    textField.placeholder = "Text here"

 }

/// add actions to the alertController

alert.addAction(ok)
alert.addAction(cancel)

/// present alertController in current viewController

self.present(alert, animated: true, completion: nil)

答案 3 :(得分:0)

首先,let itemTitle = textField.text!会使itemTitle只分配一次,并且它将包含空字符串,因为textField还没有任何值,这就是为什么当你打印出来时,它不会返回任何字符串。

如果你想要一个混乱的方法,你可以保留你的代码,但删除

let itemTitle = textField.text! 

并做@Bilal所说的