使用IB将UISegmentedControl添加到NavigationBar

问题描述:

我目前正在视图控制器的 viewDidLoad 方法中以编程方式创建 UISegmentedControl 并将其添加到视图控制器导航栏将其分配给 self.navigationItem.titleView

I'm current creating a UISegmentedControl programmatically in a view controller's viewDidLoad method and adding it to the view controller's navigation bar by assigning it to self.navigationItem.titleView.

这很容易,但我希望能够在Interface Builder中做到这一点,到目前为止还没有弄清楚如何做到这一点。谷歌也没有太多帮助。有人可以在IB中描述如何执行此操作或指向在线示例吗?我非常感激。
谢谢,
霍华德

That's easy enough, but I'd like to be able to do this in Interface Builder as well and so far haven't been able to figure out how. Google hasn't been much help either. Can someone describe how to do this in IB or point to an online example? I'd be much appreciative. Thanks, Howard

如果你的笔尖里有整个导航堆栈,它实际上是挺容易;你可以把它拖到标题区域,IB会自动做正确的事。

If you've got whole nav stack in the nib, it's actually pretty easy; you can just drag it into the title area and IB does the right thing automatically.

否则,你可以将分段控件添加到笔尖(不一定是子视图)然后从视图控制器向它添加 @property IBOutlet 。然后在viewDidLoad中,将其正常分配给titleView。记得以dealloc发布,你是金色的。

Otherwise, you can just add the segmented control to the nib (not necessarily a subview) and then add an @property IBOutlet to it from your view controller. Then in viewDidLoad, assign it to the titleView as normal. Remember to release in dealloc, and you're golden.