父视图navigationController
我有一个视图,它是 tabBarController 的一部分。在此视图中,我有一个带有表格的子视图。单击此表中的单元格时,我想访问父视图的导航控制器。这可能吗?如果可能的话,如何实现?
我以为会这样,
BandDetailViewController *bandDetailViewController = [[BandDetailViewController alloc] initWithNibName:@"BandDetailViewController" bundle:nil];
bandDetailViewController.band = [thisDay objectAtIndex:indexPath.row];
[super.navigationController pushViewController:bandDetailViewController animated:YES];
[bandDetailViewController release];
但是那行不通。
I have a view which is a part of a tabBarController. In this view I have a subview with a table. When clicking a cell in this table, I would like to access the navigationController of the parent view. Is this possible - and if so, how?
I thought it would be
BandDetailViewController *bandDetailViewController = [[BandDetailViewController alloc] initWithNibName:@"BandDetailViewController" bundle:nil];
bandDetailViewController.band = [thisDay objectAtIndex:indexPath.row];
[super.navigationController pushViewController:bandDetailViewController animated:YES];
[bandDetailViewController release];
But that does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您实例化子视图控制器时,传入对导航控制器(超级控制器)的引用,并将其存储在实例变量中。然后您可以在需要时在子中引用它。我一直在寻找更优雅的解决方案来解决这个问题和类似的问题,但没有成功。传递参考作品,所以我这样做并尝试继续我的生活。
编辑:添加代码示例
在mainVC.m中
在SubViewController.h中
在SubViewController.m中
When you instantiate your sub-view controller, pass in a reference to the navigation controller (super's), and store it in an instance variable. You can then reference it when you need it in the sub. I have been looking for a more elegant solution to this and similar problems, without success. Passing in a reference works, so I do that and try to get on with my life.
EDIT: add code sample
In mainVC.m
In SubViewController.h
In SubViewController.m
当你调用-pushViewController时,哪个视图控制器是self?如果您从选项卡子视图之一中调用它,则 self 可能没有从您添加到的顶级视图中对导航控制器的引用。
请看一下这篇文章。这家伙看起来也有同样的问题。
无法为子视图推送ViewController
When you call -pushViewController, which view controller is self? If you are calling that from within one of your tab subviews, self likely doesn't have a reference to the navigation controller from the top level view that you added it to.
Please have a look at this post. This guy looks like having same problem.
Unable to pushViewController for subview