使用未解析的标识符" performSegue"错误

时间:2017-09-09 04:04:51

标签: ios swift tableview segue

为什么Xcode告诉我performSegue(withIdentifier :)是一个未解析的标识符? (请注明注释掉的行)

import UIKit

class AllListsViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = makeCell(for: tableView)
        cell.textLabel!.text = "List \(indexPath.row)"

        return cell
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "ShowChecklist", sender: nil) // Use of unresolved identifier "performSegue"
}

func makeCell(for tableView: UITableView) -> UITableViewCell {
    let cellIdentifier = "Cell"
    if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) {
        return cell
    } else {
        return UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
    }
}

2 个答案:

答案 0 :(得分:1)

您的代码中有两个错误:

  1. 第27行UITableViewController应该在结束时结束
  2. 您继承自override,因此您需要tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) import UIKit class AllListsViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem() } // MARK: - Table view data source override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = makeCell(for: tableView) cell.textLabel!.text = "List \(indexPath.row)" return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { performSegue(withIdentifier: "ShowChecklist", sender: nil) // Use of unresolved identifier "performSegue" } func makeCell(for tableView: UITableView) -> UITableViewCell { let cellIdentifier = "Cell" if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) { return cell } else { return UITableViewCell(style: .default, reuseIdentifier: cellIdentifier) } } }
  3. 编译的代码:

    {{1}}

答案 1 :(得分:0)

尝试将UIViewController包含在课程中,这解决了我的问题:

class AllListsViewController: UITableViewController, UIViewController { 
                                                     ^^^^^^^^^^^^^^^^