繁体   English   中英

如何从动态创建的Storyboard属性为nil的UIViewController执行SegueWithIdentifier()

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

我正在一个具有搜索功能的项目。我有3个视图控制器,即HomeViewController,SearchResultViewController和DetailViewController。

在主页上,我在NavigationBar中有一个搜索栏(使用UISearchController )以及一些虚拟内容。 这里重要的是,我正在使用单独的控制器(在我的情况下是SearchResultController)来显示结果。 这个SearchResultController实际上是TableViewController的子类,它在tableview中显示结果。 到这里一切都很好。

问题

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

好的,继续我实现了didSelectRowAtIndexPath方法的问题,然后我调用了performSegueWithIdentifier,并且由于storyboard属性为nil(在文档中有提到),我收到了错误“ 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的实例以显示结果,因此其Storyboard属性为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];

您无法执行segue,因为没有情节提要不会存在segue

这样的事情很快

 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 })

快速避免任何错误!!!

希望这会有所帮助:)

暂无
暂无

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

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