初始化 UINavigationItem 的习惯用法

发布于 2024-10-21 12:46:45 字数 535 浏览 3 评论 0原文

由于 UIViewController navigationItem 出口已被弃用(是的,我有点落后),指定 UINavigationItem 元素的正确习惯用法是什么?

我已经看到了几种建议的方法,但没有一个对我来说完全清楚:

  1. “在视图控制器的 XIB 中嵌入导航项”。

  2. 使用从头开始的值在代码中初始化导航项的属性。

  3. 在视图控制器中为导航控制器创建一个插座,在视图控制器的 XIB 中连接一个导航项,并使用其属性在代码中初始化(实际)导航项的属性。

我不清楚如何“嵌入”导航项(简单地将其添加为 IB 中视图控制器的子项没有效果);我不清楚这些方法中哪种更好,或者就此而言,在哪里(以什么方法)执行 2 或 3。

Since UIViewController navigationItem outlets are deprecated (yes, I'm a bit behind), what is the correct idiom specifying the elements of a UINavigationItem?

I've seen several approaches suggested, but none are entirely clear to me:

  1. "Embed the navigation item in the view controller" in the view controller's XIB.

  2. Initialize the navigation item's properties in code, with from-scratch values.

  3. Create an outlet for the navigation controller in the view controller, wire up a navigation item in the view controller's XIB, and use its properties to initialize the (actual) navigation item's properties in code.

It isn't clear to me how to "embed" the navigation item (simply adding it as a child of the view controller in IB has no effect); and it isn't clear to me which of these approaches is better, or, for that matter, where (in what method) to do 2 or 3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

把人绕傻吧 2024-10-28 12:46:45

1) 如果控制器是在 XIB 中创建的,您可以将 UINavigationItem 放在其上并调整此项 - 它会起作用。例如,当您在 XIB 中定义 UINavigationControler 时,您可以将一些控制器作为根视图控制器放入其中。因此,您可以在 XIB 中为该控制器拥有 UINavigationItem

2) 如果控制器从 XIB 加载它的视图(它是由 alloc 创建的,然后是 initinitWithNibName:bundle:) 它在 XIB 中仅显示为 File's Owner,不支持其中的 UINavigationItem。在这种情况下,您应该在代码中配置导航项(我通常在 viewDidLoad 中进行配置)。不需要创建它,它已经存在于控制器的 navigtionItem 属性中。

self.navigationItem.title = @"Price List";

也许可以为导航项创建出口,但我不建议这样做。苹果公司宣布这种插座已经过时是有原因的。我记得有一天和同事讨论过这个问题,但我忘记了它是什么(当时很明显)。

1) If controller is created in XIB you can drop UINavigationItem on it and tweak this item - it will work. For example when you're defining UINavigationControler in XIB you can put some controller inside as root view controller. So you can have UINavigationItem for this controller in XIB.

2) If controller loads it's view from XIB (it was created by alloc and then init or initWithNibName:bundle:) it's presented in XIB only as File's Owner which doesn't support UINavigationItem in it. In this case you should configure navigation item in code (I do it in viewDidLoad usually). There is no need to create it, it's already there in navigtionItem property of your controller

self.navigationItem.title = @"Price List";

Maybe it's possible to make outlet for navigation item but I wouldn't recommend this. Apple declared such outlet obsolete for a reason. I remember discussing this with co-worker one day, but I forgot what it was (it was obvious then).

伪心 2024-10-28 12:46:45

在我们的项目中,我们要求尽可能从 IB 定制 UI。
因此,我将 UINavigationItem 添加到 xib,配置它,将其作为出口链接到我的自定义 UIViewController 子类,然后在运行时复制所有属性,并使用类别添加到 UIViewController 的方法:

- (void)setNavigationItemPropertiesFromOtherItem:(UINavigationItem *)navItem 
{
    // WORKAROUND: we can't link UINavigationItem to UIViewController from IB, and navigationItem property in UIViewController is readonly
    self.navigationItem.title = navItem.title;
    self.navigationItem.prompt = navItem.prompt;
    self.navigationItem.hidesBackButton = navItem.hidesBackButton;
    if (navItem.backBarButtonItem != nil) 
    {
        self.navigationItem.backBarButtonItem = navItem.backBarButtonItem;
    }
    if (navItem.leftBarButtonItem != nil)
    {
        self.navigationItem.leftBarButtonItem = navItem.leftBarButtonItem;
    }
    if (navItem.rightBarButtonItem != nil)
    {
        self.navigationItem.rightBarButtonItem = navItem.rightBarButtonItem;
    }
    if (navItem.titleView != nil)
    {
        self.navigationItem.titleView = navItem.titleView;
    }
}

此解决方法还允许使用 IB 将栏按钮项链接到 UINavigationItem。

On our project we have a requirement to make UI be as customizable from IB as possible.
So, I add UINavigationItem to xib, configure it, link it as outlet to my custom UIViewController subclass, and then copy all properties at runtime with method added to UIViewController using category:

- (void)setNavigationItemPropertiesFromOtherItem:(UINavigationItem *)navItem 
{
    // WORKAROUND: we can't link UINavigationItem to UIViewController from IB, and navigationItem property in UIViewController is readonly
    self.navigationItem.title = navItem.title;
    self.navigationItem.prompt = navItem.prompt;
    self.navigationItem.hidesBackButton = navItem.hidesBackButton;
    if (navItem.backBarButtonItem != nil) 
    {
        self.navigationItem.backBarButtonItem = navItem.backBarButtonItem;
    }
    if (navItem.leftBarButtonItem != nil)
    {
        self.navigationItem.leftBarButtonItem = navItem.leftBarButtonItem;
    }
    if (navItem.rightBarButtonItem != nil)
    {
        self.navigationItem.rightBarButtonItem = navItem.rightBarButtonItem;
    }
    if (navItem.titleView != nil)
    {
        self.navigationItem.titleView = navItem.titleView;
    }
}

This workaround also allows to link bar button items to UINavigationItem using IB.

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