无法为子视图推送ViewController
我有一个 UINavigationController 和我有单独的 UIViews,我使用 UISegmentControl 在它们之间切换。在切换视图时,我将视图作为子视图添加到导航控制器的视图中:
[self.view addSubview:segmentTab1.view];
然后
[self.view addSubview:segmentTab2.view];
,在子视图中,每个视图都有一个 UITableView,但我的问题是,我无法将新的 viewController 推送到 didSelectRowAtIndexPath
方法。
该方法被正确调用,通过设置断点,我可以看到推送视图的方法也被调用,但没有任何反应。这是我推送它的代码:
[self.navigationController PushViewController:detailsViewControllerAnimated:YES];
我也尝试了
[super.navigationController PushViewController:detailsViewControlleranimated:YES];
什么我做错了吗?或者只是不可能用子视图来做到这一点?
I have a UINavigationController and I have seperate UIViews that I switch between using a UISegmentControl. On switching the views, I add the view as a subview to my navigation controller's view:
[self.view addSubview:segmentTab1.view];
and
[self.view addSubview:segmentTab2.view];
Then, in the subViews, each has a UITableView, but my issue is, that I am unable to push a new viewController into view in the didSelectRowAtIndexPath
method.
The method is called correctly and by setting breakpoints, I can see the method for pushing the view gets called as well, but nothing happens. This is my code for pushing it:
[self.navigationController pushViewController:detailsViewController animated:YES];
I also tried
[super.navigationController pushViewController:detailsViewController animated:YES];
What am I doing wrong - or is is just not possible to do it with a subview?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当你调用-pushViewController时,哪个视图控制器是self?如果您从选项卡子视图之一中调用它,则 self 可能没有从您添加到的顶级视图中对导航控制器的引用。您可以通过从顶级视图中获取导航控制器的内存地址并将其与子视图中的内存地址进行比较来验证这一点。显然,如果它为零或不匹配,那就是你的问题。
当使用 -pushViewController 将其添加到导航堆栈本身时,您只能“免费”获得一个导航控制器,而您的子视图尚未以这种方式添加。
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. You can verify this by getting the memory address of the navigation controller from within the top level view and comparing it to what you have in the subview. Obviously if it's nil or doesn't match, then that's your problem.
You only get a navigation controller "for free" when it's been added to the navigation stack itself with -pushViewController which your subviews haven't been added that way.
在为所有视图实现通用标头时,我遇到了类似的问题
经过多次尝试,我已经通过此修复了它 -
在所有 viewController 中,
我都引用了以下帖子来实现标题视图
iPhone 中多个视图控制器中的通用 XIB
[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] viewWillAppear:YES]; // 这是调用 HeaderController 的 viewWillAppear 方法,我们可以在其中编写代码来检查用户是否已登录,并将登录按钮更改为注销按钮等。
I had a similar issue when implementing a common header for all the views
After many tries , i have fixed it by this -
In all the viewController
I have referred following post to implement the header view
Common XIB in multiple View Controller in iPhone
[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] viewWillAppear:YES]; // this is to call the viewWillAppear method of HeaderController where we can write code to check the user is logged or not and change the login button to logout button etc ..
不是 和
,并使
[super.navigationController PushViewController:detailsViewControllerAnimated:YES];
工作。您可以使用
和
将 viewController 添加到导航层次结构中,而
Instead of
and
you may use
and
to add your viewControllers to the navigation hierarchy, and make
[super.navigationController pushViewController:detailsViewController animated:YES];
work.我并不完全清楚你的视图层次结构是什么,但一般来说,如果你的导航控制器视图不是窗口的第一个子视图或Apple集合视图之一的元素(另一个导航视图控制器的内容视图或选项卡控制器的内容视图)它将无法正常工作。
It is not entirely clear to me what your view hierarchy is, but in general if your navigation controllers view is not the first subview of a window or an element of one of Apple's collection views (either another navigation view controller's content view or a tab controller's content view) it won't work correctly.
如果您不反对单例,一种可能性是使产品的 UINavigationController 对象成为单例,可以从(例如)您的应用程序委托访问。
您可以这样调用它:
在 MyApplicationDelegate 中,navController 返回单例对象。
One possibility, if you are not averse to singletons, is to make your product's UINavigationController object be a singleton, accessible from (for example) your application delegate.
You would invoke it thus:
where within MyApplicationDelegate, navController returns the singleton object.