简体   繁体   English

从UITableViewCell到DetailViewController的选择不起作用

[英]segue from UITableViewCell to DetailViewController not working

I've got this problem where when running my app on the iOS simulator, a segue I have from a custom table view cell to a detail view controller doesn't work. 我遇到了一个问题,即在iOS模拟器上运行我的应用程序时,从自定义表格视图单元到详细视图控制器的设置无法正常工作。 I can click on the cell but then the app freezes and I can no longer do anything without having to stop running the app and restart it. 我可以单击该单元格,但随后该应用程序冻结,并且无需停止运行该应用程序并重新启动它就无法执行任何操作。 I have such segues in a few places in my app, and only one of them works fine, the others don't. 我的应用程序中有几个地方存在此类问题,但只有其中一个能正常工作,而其他则不能。 I've made sure that the segue identifier is okay, still nothing works. 我已经确定segue标识符是可以的,但仍然没有任何效果。 Any ideas on why this is happening? 为什么会这样?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showDetail" {

        let cell = sender as! UITableViewCell
        let indexPath = tableView.indexPathForCell(cell)

        let detailViewController: DetailTableViewController = segue.destinationViewController as! DetailTableViewController

        let objectAtIndex: Tasks = fetchedResultsController.objectAtIndexPath(indexPath!) as! Tasks

        detailViewController.receivingObject = objectAtIndex

    }

}

Add this extension to by pass nav controllers while segueing 在segueing时将此扩展名添加到通过导航控制器

extension UIViewController{
    var visibleVC: UIViewController {
        if self is UINavigationController{
            if let cvc = (self as! UINavigationController).visibleViewController { return cvc }
        }
        return self 
    }
}

and try this prepare segue func 并尝试此准备segue func

if segue.identifier == "showDetail" {
        if let cell = sender as! UITableViewCell{
             let indexPath = tableView.indexPathForCell(cell)
             if let let detailViewController = segue.destinationViewController. visibleVC as! DetailTableViewController{
                  if let objectAtIndex = fetchedResultsController.objectAtIndexPath(indexPath!) as! Tasks{
                      detailViewController.receivingObject = objectAtIndex
                  } else { print("unlikely") }
             } else { print("wiring problem in storyboard") }
        } else { print("segue is not initiated from a cell") }
 }

This will at list make it easier to figure out where the problem is but I think its just the nav vc getting in the way. 这将使列表更容易找出问题出在哪里,但我认为这只是导航vc造成的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM