如何从动态创建的 UIViewController 执行 SegueWithIdentifier(),其故事板 属性 为 nil

How to performSegueWithIdentifier() from dynamically created UIViewController whose storyboard property is nil

我正在开发一个搜索 functionality.I 有 3 个视图控制器的项目,即 HomeViewController、SearchResultViewController 和 DetailViewController。

在主页面上,我在导航栏中有一个搜索栏(使用 UISearchController)以及一些虚拟内容。这里重要的是我使用单独的控制器(在我的例子中是 SearchResultController)来显示结果。这个 SearchResultController 实际上是 TableViewController 的子类,它在 tableview 中显示结果。到这里为止一切正常。

问题

现在,当我从结果中选择任何行时(即在 SearchResultViewController 中),我想打开另一个视图控制器 (DetailViewController),我将在其中显示有关该所选项目的详细信息。这是一件非常简单的事情,但我被困在这里。

好的,继续我已经实现了 didSelectRowAtIndexPath 方法的问题,并从中调用了 performSegueWithIdentifier,但出现错误“Receiver has no segue with identifier”,因为情节提要 属性 为零(在文档中提到)。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("storyboard property is \(self.storyboard)")
        performSegueWithIdentifier(segueIdentifier, sender: indexPath.row)
    }

注: segueIdentifier 与故事板上的相同。 我得到那个故事板 属性 是零。

我猜

当我点击搜索栏时,iOS 在内部创建并加载 SearchResultController 的实例以显示结果,因此它的情节提要 属性 为 nil,因此我得到以上错误。

有人可以解决此问题或提供正确方向的任何指示。

如果您正在以编程方式创建 viewcontroller,那么您应该像

这样的导航控制器
  UIViewController *vc = [[UIViewController alloc]init]; //your viewcontroller here

[self.navigationController pushViewController:vc animated:YES];

//or if you want to present it then

[self presentViewController:vc animated:YES completion:nil];

你不能执行转场,因为没有故事板就没有转场

在 swift

中有类似的内容
 let vc: UIViewController = UIViewController()
    //your viewcontroller here
    self.navigationController!.pushViewController(vc, animated: true)
    //or if you want to present it then
    self.presentViewController(vc, animated: true, completion: { _ in })

避免在swift中出现任何错误!!!

希望这会有所帮助:)