设置在 Interface Builder 中创建的导航栏的标题
我有一个模态视图控制器,其视图来自 XIB。我希望它与基于导航的应用程序的其余部分具有相同的外观和感觉,但它不是推送到导航堆栈的视图,而是以模态方式呈现。我拖入一个 UINavigationBar 并将其设为出口,但它没有要设置的标题属性。我有两个字段可以显示此模态视图,并且我希望根据创建模态视图的字段来设置不同的标题。
I have a modal view controller whose view comes from a XIB. I want it to have the same look and feel as the rest of my navigation based app, but it's not a view that's pushed to the navigation stack, it's presented modally. I dragged a UINavigationBar in and made it an outlet, but it doesn't have a title property to set. I have two fields that can bring up this modal view, and I want the title set differently depending on which one creates the modal view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
UINavigationBar
管理一堆UINavigationItem
,就像UINavigationController
管理一堆UIViewController
一样。要设置直接可见的内容,您应该使用pushNavigationItem:animated:
或setItems:animated:
以及您希望栏反映的视图控制器的 navigationItem。例如:
在上面的代码中,您有一个引用独立导航栏的属性
navigationBar
。如果您不想自己管理它,您可以按照 mplappert 的建议进行操作,并将视图控制器(没有独立的
UINavigationBar
)嵌套在UINavigationController
中,并呈现导航控制器以模态方式代替视图控制器。UINavigationBar
's manage a stack ofUINavigationItem
s much like aUINavigationController
manager a stack ofUIViewController
s. To set what is visible directly, you should use eitherpushNavigationItem:animated:
orsetItems:animated:
using the navigationItem of the view controller you want the bar to reflect.eg:
The above code where you have a property
navigationBar
which references the stand-alone navigation bar.If you don't want to manage it yourself, you can do as mplappert suggested and nest your view controller (without a stand-alone
UINavigationBar
) in aUINavigationController
and present the navigation controller modally instead of your view controller.如果您已经在 IB 中定义了 UINavigationBar,也会发现这种更简单的方法。假设 self.navigationBar 是您的 IBOutlet:
Also found this easier way if you've already defined a UINavigationBar in your IB. Assuming self.navigationBar is your IBOutlet:
(使用 XCode 4.5.2)找到了一种直接在 Interface Builder 中工作的方法 - 无需在代码中设置。
在 (IB) 中选择 ViewController 对象后,在 Identity Inspector 下有一个标记为“用户定义的运行时属性”的部分。
我添加了 String 类型的 keyPath“标题”并将其设置为我想要的值。
经过测试,当推送到导航控制器堆栈上并以模态方式打开时(当然使用模态导航控制器),这可以工作。
(Using XCode 4.5.2) Found a method that works directly in Interface Builder - without setting in code.
With the ViewController object selected in (IB), under the Identity Inspector there is a section labeled "User Defined Runtime Attributes".
I added the keyPath "title" of type String and set it to the value I wanted.
Tested and this works when pushed onto a Nav Controller stack and when opened modally (with a Modal Nav Controller of course).
viewcontroller.h 文件中创建导航项的 iboutlet
- 在 viewcontroller.m 文件中将其命名为“navigationBarTitle”
,在超级 viewDidLoad 中添加代码,
将导航栏项中的 Storybord 连接到 navigationBarTitle。
viewcontroller.h file in create iboutlet of the navigation item
- Named it "navigationBarTitle"
in viewcontroller.m file in add the code in super viewDidLoad
connect the storybord in navigation bar item to navigationBarTitle.
在我的 .h 文件中
将导航项的子项从故事板拖到 .h 文件中
将其命名为“navigationBarTitle”
在我的 .m 文件中将
in my .h file
Dragged the Child of the navigation item from the storyboard into the .h file
Named it "navigationBarTitle"
in my .m file
这是最简单的方法:
Here is the Simplest way:
您可以通过以下代码使用现有的导航栏
you can use existing navigation bar with following code
只需将您的 UIViewController 放入新的 UINavigationController 视图控制器堆栈中并以模态方式呈现该导航控制器即可。
Simply put your
UIViewController
into a newUINavigationController
s view controller stack and present that navigation controller modally.