在Swift中展示UIAlertController中的textField

5

刚刚我写了下面提供的代码,但是当我运行它时,它崩溃并显示 nil,我希望能够输出以下代码。

class ViewController: UIViewController ,UITableViewDataSource{
var names = [String]()

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    title = "\"The List\""
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func addName(sender: AnyObject) {
    let alert = UIAlertController(title: "New name", message: "Add one new name here", preferredStyle: .Alert)
    let saveAction = UIAlertAction(title: "Save", style: .Default, handler: {(action:UIAlertAction) ->Void in
        alert.addTextFieldWithConfigurationHandler{(textField:UITextField)->Void in
            alert.view.endEditing(true)
            let textField = alert.textFields?.last
            self.names.append(textField!.text!)
            self.tableView.reloadData()
        }

    })
    let cancleAction = UIAlertAction(title: "Cancel", style: .Default, handler: {(action:UIAlertAction) ->Void in
        alert.addTextFieldWithConfigurationHandler {(textFiled:UITextField) ->Void in
        }
                })
    alert.addAction(saveAction)
    alert.addAction(cancleAction)
    self.presentViewController(alert,animated:true,completion:nil)

}

    //MARK:-TableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return names.count

}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
    cell?.textLabel?.text = names[indexPath.row]
    return cell!

}
}

如何解决它?
2个回答

19

UIALertController中添加textField的代码:

let alertController = UIAlertController(title: "PlainTextStyle", message: "PlainTextStyle AlertView.", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField) -> Void in
            textField.placeholder = "Login"
        }
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (result : UIAlertAction) -> Void in
            print("Cancel")
        }
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
           print(alertController.textFields?.first?.text)
        }
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)

更新 Swift 3.0

 let alertController = UIAlertController(title: "PlainTextStyle", message: "PlainTextStyle AlertView.", preferredStyle: UIAlertControllerStyle.alert)
        alertController.addTextField { (textField : UITextField) -> Void in
            textField.placeholder = "Login"
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (result : UIAlertAction) -> Void in
        }
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
        }
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)

UIAlertController中的TextField:

输入图像描述


好的,我知道根本原因了。首先,我创建了一个alertController,但我忘记配置textField了......所以最终我看不到它。对吗? - Marc Steven

0
错误在于我忘记将textfield添加到AlertController中...太简单了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接