【问题标题】:How can I get the text from the alert's textfield and make it the cell's text label?如何从警报的文本字段中获取文本并将其设为单元格的文本标签?
【发布时间】:2017-01-07 22:26:05
【问题描述】:
class CoursesTableViewController: UITableViewController {
    var nameOfCourse = "";

    @IBAction func newCourse(_ sender: AnyObject) {
            //1. Create the alert controller.
            // var nameOfCourse = "";
            let alert = UIAlertController(title: "New Course", message: "Enter the course name.", preferredStyle: .alert);

            //2. Add the text field. You can configure it however you need.
            alert.addTextField(configurationHandler: { (textField) -> Void in
                textField.placeholder = ""
            })

            //3. Grab the value from the text field, and print it when the user clicks OK.
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
                let textField = alert.textFields![0] as UITextField

                self.nameOfCourse = textField.text!

                print(self.nameOfCourse)
            }))

            alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
            }))

            // 4. Present the alert.
            self.present(alert, animated: true, completion: nil)
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cells = tableView.dequeueReusableCell(withIdentifier: "cells", for: indexPath) as? CoursesTableViewCell

        cells?.textLabel.text = self.nameOfCourse

        return cells!
    }

【问题讨论】:

    标签: ios swift uitableview uialertcontroller


    【解决方案1】:

    成功设置nameOfCourse(在print(self.nameOfCourse)下方)后重新加载表格视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      相关资源
      最近更新 更多