设置在 Interface Builder 中创建的导航栏的标题

发布于 2024-10-11 15:34:15 字数 168 浏览 3 评论 0原文

我有一个模态视图控制器,其视图来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

眼中杀气 2024-10-18 15:34:15

UINavigationBar 管理一堆 UINavigationItem,就像 UINavigationController 管理一堆 UIViewController 一样。要设置直接可见的内容,您应该使用 pushNavigationItem:animated:setItems:animated: 以及您希望栏反映的视图控制器的 navigationItem。

例如:

self.navigationItem.title = @"A custom title";
[self.navigationBar pushNavigationItem:self.navigationItem animated:NO];

在上面的代码中,您有一个引用独立导航栏的属性navigationBar

如果您不想自己管理它,您可以按照 mplappert 的建议进行操作,并将视图控制器(没有独立的 UINavigationBar)嵌套在 UINavigationController 中,并呈现导航控制器以模态方式代替视图控制器。

UINavigationBar's manage a stack of UINavigationItems much like a UINavigationController manager a stack of UIViewControllers. To set what is visible directly, you should use either pushNavigationItem:animated: or setItems:animated: using the navigationItem of the view controller you want the bar to reflect.

eg:

self.navigationItem.title = @"A custom title";
[self.navigationBar pushNavigationItem:self.navigationItem animated:NO];

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 a UINavigationController and present the navigation controller modally instead of your view controller.

一杆小烟枪 2024-10-18 15:34:15

如果您已经在 IB 中定义了 UINavigationBar,也会发现这种更简单的方法。假设 self.navigationBar 是您的 IBOutlet:

self.navigationBar.topItem.title = title;

Also found this easier way if you've already defined a UINavigationBar in your IB. Assuming self.navigationBar is your IBOutlet:

self.navigationBar.topItem.title = title;
风启觞 2024-10-18 15:34:15

(使用 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).

话少心凉 2024-10-18 15:34:15

viewcontroller.h 文件中创建导航项的 iboutlet

     @property (weak, nonatomic) IBOutlet UINavigationItem *navigationBarTitle;

- 在 vi​​ewcontroller.m 文件中将其命名为“navigationBarTitle”

,在超级 viewDidLoad 中添加代码,

      self.navigationBarTitle.title = @"Favorites";

将导航栏项中的 Storybord 连接到 navigationBarTitle。

viewcontroller.h file in create iboutlet of the navigation item

     @property (weak, nonatomic) IBOutlet UINavigationItem *navigationBarTitle;

- Named it "navigationBarTitle"

in viewcontroller.m file in add the code in super viewDidLoad

      self.navigationBarTitle.title = @"Favorites";

connect the storybord in navigation bar item to navigationBarTitle.

放飞的风筝 2024-10-18 15:34:15

在我的 .h 文件中

  • 将导航项的子项从故事板拖到 .h 文件中

  • 将其命名为“navigationBarTitle”

在我的 .m 文件中将

  • 添加 self.navigationBarTitle.title = @"Some Title";

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

  • Added self.navigationBarTitle.title = @"Some Title";
仅冇旳回忆 2024-10-18 15:34:15

这是最简单的方法:

[[[self.navigationController navigationBar] topItem] setTitle:@"The Title"];

Here is the Simplest way:

[[[self.navigationController navigationBar] topItem] setTitle:@"The Title"];
冰火雁神 2024-10-18 15:34:15

您可以通过以下代码使用现有的导航栏

self.navigationController?.navigationBar.topItem?.title = "Calender"

you can use existing navigation bar with following code

self.navigationController?.navigationBar.topItem?.title = "Calender"
梦幻的味道 2024-10-18 15:34:15

只需将您的 UIViewController 放入新的 UINavigationController 视图控制器堆栈中并以模态方式呈现该导航控制器即可。

Simply put your UIViewController into a new UINavigationControllers view controller stack and present that navigation controller modally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文