初始化 UINavigationItem 的习惯用法
由于 UIViewController
navigationItem 出口已被弃用(是的,我有点落后),指定 UINavigationItem
元素的正确习惯用法是什么?
我已经看到了几种建议的方法,但没有一个对我来说完全清楚:
“在视图控制器的 XIB 中嵌入导航项”。
使用从头开始的值在代码中初始化导航项的属性。
在视图控制器中为导航控制器创建一个插座,在视图控制器的 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:
"Embed the navigation item in the view controller" in the view controller's XIB.
Initialize the navigation item's properties in code, with from-scratch values.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 如果控制器是在 XIB 中创建的,您可以将
UINavigationItem
放在其上并调整此项 - 它会起作用。例如,当您在 XIB 中定义 UINavigationControler 时,您可以将一些控制器作为根视图控制器放入其中。因此,您可以在 XIB 中为该控制器拥有UINavigationItem
。2) 如果控制器从 XIB 加载它的视图(它是由
alloc
创建的,然后是init
或initWithNibName:bundle:
) 它在 XIB 中仅显示为File's Owner
,不支持其中的UINavigationItem
。在这种情况下,您应该在代码中配置导航项(我通常在viewDidLoad
中进行配置)。不需要创建它,它已经存在于控制器的navigtionItem
属性中。也许可以为导航项创建出口,但我不建议这样做。苹果公司宣布这种插座已经过时是有原因的。我记得有一天和同事讨论过这个问题,但我忘记了它是什么(当时很明显)。
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 definingUINavigationControler
in XIB you can put some controller inside as root view controller. So you can haveUINavigationItem
for this controller in XIB.2) If controller loads it's view from XIB (it was created by
alloc
and theninit
orinitWithNibName:bundle:
) it's presented in XIB only asFile's Owner
which doesn't supportUINavigationItem
in it. In this case you should configure navigation item in code (I do it inviewDidLoad
usually). There is no need to create it, it's already there innavigtionItem
property of your controllerMaybe 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).
在我们的项目中,我们要求尽可能从 IB 定制 UI。
因此,我将 UINavigationItem 添加到 xib,配置它,将其作为出口链接到我的自定义 UIViewController 子类,然后在运行时复制所有属性,并使用类别添加到 UIViewController 的方法:
此解决方法还允许使用 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:
This workaround also allows to link bar button items to UINavigationItem using IB.