使用容器视图控制器访问导航栏 [英] accessing navigation bar with container view controller

查看:22
本文介绍了使用容器视图控制器访问导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 UINavigationController 和 UINavigatinBar 编写自己的容器时的两个问题.

Two questions regarding UINavigationController and UINavigatinBar when writing your own container.

1) 我用故事板创建了我的初始容器并将其嵌入到 UINavigationController 中.我发现我的 childViewControllers 访问 UINavigationBar 的唯一方法是在 childViewController 的 viewDidLoad 中做这样的事情:

1) I created my initial container with a storyboard and embedded it in a UINavigationController. The only way I found for my childViewControllers to access the UINavigationBar was doing something like this in the childViewController's viewDidLoad:

UIBarButtonItem *showDetailViewControllerBBI = [[UIBarButtonItem alloc] initWithTitle:@"Show Details" style:UIBarButtonItemStyleBordered target:self action:@selector(switchToCarouselLayout:)];
self.parentViewController.navigationItem.leftBarButtonItem = showDetailViewControllerBBI;

这是获取 UINavigationBar 的正确方法吗?

Is this the proper way to get the UINavigationBar?

2) 我想我可以将 UINavigationBar 放在我最初的 ContainerViewController 上.我所做的是删除故事板中的 UINavigatinController 并将 UINavigatinBar 放在我的 containerViewController 的顶部.当我运行项目时,没有显示导航栏.那么如果我不能在初始容器视图控制器中使用 IB 中的 UINavigationBar,而不嵌入到 UINavigationController 中,它实际上会做什么?或者它只是为了美观而您只能将它与 UINavigationControllers 一起使用?谢谢.

2) I thought I could slap in a UINavigationBar onto my initial ContainerViewController. What I did was delete the UINavigatinController in my storyboard and put a UINavigatinBar at the top of my containerViewController. When I run the project, no navigation bar shows. So what does the UINavigationBar in IB actually do if I can't have it in my initial container view controller, without embedding in the UINavigationController? Or is it just for aesthetics and you can only use it with UINavigationControllers? Thanks.

推荐答案

1) 是的,我认为这是从子视图控制器引用某些内容的好方法.另一种方法是在容器控制器本身中定义该栏按钮项,然后将目标设置为 self.childViewControllers.lastObject(假设只有一个).

1) Yes, I think that is an OK way to reference something from a child view controller. Another way, would be to define that bar button item in the container controller itself and then set the target to self.childViewControllers.lastObject (assuming there's only one).

2) 这应该有效.您是在导航栏中拖动,还是只是将顶部栏的模拟指标更改为导航栏?后者仅用于 IB 中的可视化布局,据我所知——它实际上并没有添加.

2) This should work. Did you drag in a navigation bar, or just change the simulated metrics of the top bar to navigation bar? The latter is only for visual layout in IB, as far as I can tell -- it doesn't actually add one.

这篇关于使用容器视图控制器访问导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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