简体   繁体   English

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

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

I'm working on a project with has a search functionality.I've 3 view controllers ie HomeViewController, SearchResultViewController and DetailViewController. 我正在一个具有搜索功能的项目。我有3个视图控制器,即HomeViewController,SearchResultViewController和DetailViewController。

On the main page, I've a search bar (using UISearchController ) within the navigationBar along with some dummy content. 在主页上,我在NavigationBar中有一个搜索栏(使用UISearchController )以及一些虚拟内容。 Important thing here is that I'm using separate controller (in my case it is SearchResultController) to show the results. 这里重要的是,我正在使用单独的控制器(在我的情况下是SearchResultController)来显示结果。 This SearchResultController is actually subclass of TableViewController which displays the result in tableview. 这个SearchResultController实际上是TableViewController的子类,它在tableview中显示结果。 Everything is working fine till here. 到这里一切都很好。

Problem 问题

Now, when I'm selecting any row from the result ( ie at SearchResultViewController) I want to open another view controller (DetailViewController) where I'll be showing the details about this selected item. 现在,当我从结果中选择任何行时(即在SearchResultViewController处),我想打开另一个视图控制器(DetailViewController),在其中将显示有关此选定项的详细信息。 It is a very simple thing but I'm stuck here . 这是很简单的事情,但是我被困在这里。

Okay, continuing with the problem I've implemented didSelectRowAtIndexPath method and from that I'm calling performSegueWithIdentifier and I'm getting error “ Receiver has no segue with identifier ” because the storyboard property is nil (It is mentioned in the documentation). 好的,继续我实现了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)
    }

Note: segueIdentifier is same as it on the storyboard. 注意: segueIdentifier与情节提要上的相同。 I'm getting that storyboard property is nil. 我得到的故事板属性为零。

I guess 我猜

When I tap on the search bar, iOS internally creates and loads the instance of the SearchResultController to display the result and therefore its storyboard property is nil and because of that I'm getting the above error. 当我点击搜索栏时,iOS在内部创建并加载SearchResultController的实例以显示结果,因此其Storyboard属性为nil,因此,我遇到了以上错误。

Can somebody give solution to this problem or any pointer to the right direction. 有人可以解决这个问题吗?还是任何指向正确方向的指针。

If you are creating viewcontroller programmatically then you should push in on navigation controller like, 如果您以编程方式创建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];

You can't perform segue because there is no segue without storyboard 您无法执行segue,因为没有情节提要不会存在segue

Something like this in 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 })

Avoid any mistake in swift!!! 快速避免任何错误!!!

hope this will help :) 希望这会有所帮助:)

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

相关问题 如何初始化一个Nib是Storyboard的UIViewController - How to init a UIViewController whose Nib is the Storyboard 在Storyboard中创建的UIViewController中动态调整UITableView的大小 - Dynamically resize UITableView in UIViewController created in Storyboard 故事板属性是UIViewController的属性吗? - Is the storyboard property an attribute of UIViewController? 新创建的UIViewController在情节提要中不可用 - newly created UIViewController is not available in a storyboard 如何从故事板以编程方式加载 UIViewController? - How to load UIViewController programmatically from storyboard? 如何使用故事板中的视图以编程方式创建UIViewController - How to Create a UIViewController programmatically with view from storyboard 如何以编程方式从UINavigationController(storyboard)到UIViewController - How to segue from UINavigationController (storyboard) to UIViewController programmatically 从情节提要的UIViewController动态添加/删除UIViews - Add/Remove dynamically UIViews from a Storyboard's UIViewController 如何清理Storyboard Segues创建的UIViewController实例? - How do I clean up UIViewController instances created by Storyboard Segues? 动态定位从StoryBoard创建的UITableView - Dynamically position a UITableView created from StoryBoard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM