Swift 4中prepare(for segue:)方法未被调用

3

我有一个分屏视图控制器,当我在表格视图中点击一个项目时,它没有调用我的prepare(for segue:)方法。这是我的prepare(for segue:)方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        print("Here");
        if segue.identifier == "showPOsDetail" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let object = objects[indexPath.row] as! NSDate
                let controller = (segue.destination as! UINavigationController).topViewController as! POsDetail
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }
    }

以下是我制作的storyboard截图。我不知道为什么这个方法没有被调用。

enter image description here

我已经试图解决这个问题好几天了,但非常沮丧它还是无法正常工作。

这是完整的主控制器代码:

import UIKit

class POsMaster: UITableViewController {

    var POsDetailController: POsDetail? = nil
    var objects = [Any]()


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
        navigationItem.leftBarButtonItem = editButtonItem

        let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
        navigationItem.rightBarButtonItem = addButton
        if let split = splitViewController {
            let controllers = split.viewControllers
            POsDetailController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? POsDetail
        }

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc
    func insertNewObject(_ sender: Any) {
        objects.insert(NSDate(), at: 0)
        let indexPath = IndexPath(row: 0, section: 0)
        tableView.insertRows(at: [indexPath], with: .automatic)
    }

    // MARK: - Segues

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        print("Here");
        if segue.identifier == "showPOsDetail" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let object = objects[indexPath.row] as! NSDate
                let controller = (segue.destination as! UINavigationController).topViewController as! POsDetail
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }
    }

    // MARK: - Table View

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "POCell", for: indexPath)

        let object = objects[indexPath.row] as! NSDate
        cell.textLabel!.text = object.description
        return cell
    }

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            objects.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        }
    }


}

是的,我有,它根本没有进入方法。 - user979331
在哪个点它不进去了??你这里有值吗?-: 如果let indexPath = tableView.indexPathForSelectedRow{},还是nil?或者它甚至没有输出吗?print("Here"); - Tushar Sharma
你的代码中在哪里推入新的 UIViewController?问题可能出在你的操作方式上。你是否使用了 UITableViewDelegate 中的 didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 方法? - Stefan Stefanov
@FrancescoDeliro 我怎么能知道/做到这件事呢? - user979331
@user979331 完成了!不用谢!很高兴能帮助你 ;) - Francesco Deliro
显示剩余8条评论
2个回答

3
在Storyboard中连接Segue时,弹出一个窗口,你需要检查是否已经从“选择组”而不是“附件组”设置了Segue。如果连接到“选择组”,则会调用你的prepareForSegue:方法。

1
太棒了,伙计! - user979331
1
嘿,抱歉打扰你,我从我的故事板中进行了segue,但我没有得到你的答复,我仍然有这个问题,你能否给我更多信息? - Arash Afsharpour
1
嗨@ArashAfshar,你是对的,我会更新我的答案以使其更清晰,谢谢你指出。无论如何,您可以在问题评论中找到解释;) - Francesco Deliro

1

您需要确保isUserEnteractionEnabled设置为true。

看起来这可能是唯一的原因。


@user979331,你能看到单元格被选中了吗(它应该变暗)? - Xcoder

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