快速显示alertController中的textField [英] show the textField in the alertController in swift

查看:42
本文介绍了快速显示alertController中的textField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我编写了下面提供的代码.我想为以下代码生成输出,但是在运行它时会崩溃,并且显示 nil .

Just now I wrote the code which is provided below. I would like to produce the output for the following code but it crashes when I run it, and it shows 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的代码:

Code for Adding textField in UIALertController :

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

Update 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆