无法为子视图 pushViewController [英] Unable to pushViewController for subview

查看:16
本文介绍了无法为子视图 pushViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UINavigationController,我有单独的 UIView,我在使用 UISegmentControl 之间切换.在切换视图时,我将视图作为子视图添加到导航控制器的视图中:

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];

[self.view addSubview:segmentTab2.view];

然后,在 subViews 中,每个都有一个 UITableView,但我的问题是,我无法在 didSelectRowAtIndexPath 方法中将新的 viewController 推送到视图中.

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];

我也试过了

[super.navigationController pushViewController:detailsViewController animated:YES];

我做错了什么 - 或者只是不可能用子视图来做?

What am I doing wrong - or is is just not possible to do it with a subview?

推荐答案

调用-pushViewController时,self是哪个视图控制器?如果您在其中一个选项卡子视图中调用它,则 self 可能没有从您添加到的顶级视图中对导航控制器的引用.您可以通过从顶级视图中获取导航控制器的内存地址并将其与子视图中的内容进行比较来验证这一点.显然,如果它是 nil 或不匹配,那是你的问题.

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.

只有使用 -pushViewController 将导航控制器添加到导航堆栈本身时,您才能免费"获得导航控制器,而您的子视图尚未以这种方式添加.

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.

这篇关于无法为子视图 pushViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆