ios - 在 swift 中显示 alertController 中的 textField

标签 ios swift uialertcontroller

刚才我写了下面提供的代码。我想为以下代码生成输出,但是当我运行它时它崩溃了,并且它显示 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!

}
}

如何解决???

最佳答案

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 中的文本字段:

enter image description here

关于ios - 在 swift 中显示 alertController 中的 textField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37696485/

相关文章:

swift 4 : Non-nominal type 'T' does not support explicit initialization

swift - AlamoFire GET api 请求未按预期工作

objective-c - 删除 UITableView 单元格之前发出警报 - (UIAlertController) Swift 或 Objective-C

ios - 在 AppDelegate 之外,我如何在基本上 `performSegueWIthIdentifier` 之前在 UINavigationController 中包含一个 UIViewController?

javascript - 在 iframe 中输入字段会导致 iframe 滚动到底部

ios - 带有嵌套数据的 Swift 4 JSON 解码器

ios - IOS 9.1/Xcode 7.1.1 中的蓝牙损坏

swift , 得到驱动器 图标

ios - POST 请求后 self.present 不起作用

ios - 何时何地在 Swift 中关闭 UIAlertController?