UINavigationController 的多个根视图控制器?
我试图作为导航控制器的根在多个表视图之间切换。根据我的应用程序的设置,我想通过不同的方法使用不同的数据集,并且更喜欢将它们封装在单独的类中。
我的想法是设置一个视图管理器类(UIViewController)作为导航控制器的根视图控制器。在视图管理器中,我们检查设置以查看要加载哪个视图:
if([application_mode intValue]==APPLICATION_MODE_A){
AViewController *aView = [[DeviceTableViewController alloc] init];
[self.view insertSubview:aView.view atIndex:0];
}
else if([application_mode intValue]==APPLICATION_B){
BViewController *bView = [[BViewController alloc] init];
[self.view insertSubview.bView.view atIndex:0];
}
实际上,这确实将适当的视图插入到视图管理器中,但代价是插入视图顶部有一个白色条,并且没有导航信息bar,即子视图未连接到导航控制器。
执行此操作的正确方法是什么?我真的不想拥有一张巨大的桌子视图!
I'm trying to switch between several table views as the root of a navigation controller. Depending on the settings of my app, I want to use different sets of data with different methods, and prefer to have these encapsulated in separate classes.
My thought was to set a view manager class (UIViewController) as the root view controller of the navigation controller. In the view manager we check the settings to see which view we want to load:
if([application_mode intValue]==APPLICATION_MODE_A){
AViewController *aView = [[DeviceTableViewController alloc] init];
[self.view insertSubview:aView.view atIndex:0];
}
else if([application_mode intValue]==APPLICATION_B){
BViewController *bView = [[BViewController alloc] init];
[self.view insertSubview.bView.view atIndex:0];
}
That does in fact insert the appropriate view into the view manager, at the cost of a white bar at the top of the inserted view and no info on the navigation bar, ie the subview is not connected to the navigation controller.
What's the proper way to do this? I'd really prefer not to have one ginormous table view!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在哪里设置你的navigationController的
rootViewController
?此时不能直接将其设置为AviewController
的对象或BViewController
的对象吗?您可能不需要我在开始时所做的中间
UIViewController
:Where do you set your navigationController's
rootViewController
? Can't you just set it to anAviewController
's object or anBViewController
's object at this time ? You may not need an intermediateUIViewController
I would do at the beginning :
由于没有视图控制器遏制,我喜欢 Jonah William 博客中概述的方法:
http://blog.carbon Five.com/2011/03/09/abusing-uiviewcontrollers/
您无法有效地将视图控制器放置在另一个视图控制器中;相反,我们创建具有类似生命周期方法(viewDidLoad、viewDidAppear 等)的东西,并将这些方法从父级转发到子级。 将其作为子视图添加到父视图中
这个“psudo-viewcontroller”有一个视图属性,我们使用 UIView addSubView http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/addSubview :
通过这种方法,我们可以封装视图元素,在视图控制器中动态切换它们,将多个视图元素放置在单个视图控制器中,等等。这样就可以将它们与导航堆栈分开考虑。这需要一些工作,但在我看来,这是 iOS 4 中最干净的 UI 封装方法。
Since there is no view controller containment, I like the approach outlined in Jonah William's blog:
http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/
You can't effectively place a view controller inside another; instead, we create something with similar lifecycle methods (viewDidLoad, viewDidAppear, etc) and forward those methods from the parent to the child. This 'psudo-viewcontroller' has a view property that we add as a subview to the parent's view, using UIView addSubView
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/addSubview:
With this approach, we can encapsulate view elements, switch them out dynamically in a view controller, place several within a single view controller, etc. This way they can be considered separately from your navigation stack. It's a bit of work, but the cleanest UI encapsulation approach in iOS 4 in my opinion.