【问题标题】:How to modify a UIBarButtonItem view programmatically after it's been setup in Interface Builder?在 Interface Builder 中设置后如何以编程方式修改 UIBarButtonItem 视图?
【发布时间】:2011-04-13 18:10:14
【问题描述】:

我在 Interface Builder 中创建了一个 UIBarButton 项并将其链接到类中的 UIBarButton 项属性。在 Interface Builder 中,它是 Style = PlainIdentifier = Custom,它的 Title 是空白的

在 viewDidLoad 方法的类文件中,我正在尝试向此 UIBarButtonItem 属性添加自定义视图。

例如

UISegmentedControl *newButton = [[UISegmentedControl alloc] initWithItems:....];
newButton.momentary = YES;
newButton.segmentedControlStyle = UISegmentedControlStyleBar;
newButton.tintColor = [UIColor .....];

[self.myBarButtonItem setCustomView:newButton];

这会导致 NOTHING 出现。这是为什么呢?

我读到如果我以编程方式创建 UIBarButtonItem:

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:newButton];

然后将此 BarButtonItem 添加到工具栏 - 它会起作用。我的问题是我的工具栏上有很多东西,这个 UIBarButton 项目需要在工具栏的最右边,如果我不能直接在界面构建器中链接到它,那么我必须构建我的所有工具栏项目以编程方式获得我需要的布局。

那么当链接到在 IB 中创建的 UIBarButtonItem 时,有没有办法使用 customView 来做到这一点?

谢谢!

【问题讨论】:

  • 我用我喜欢使用的方法编辑了我的答案,这样我就不再像一个便宜的混蛋了;)

标签: iphone cocoa-touch ios4 uibarbuttonitem uitoolbar


【解决方案1】:

要更改 UIToolBar 上的内容,请使用它的 items 属性。那应该回答你的问题:How to programmatically replace UIToolBar items built in IB

编辑:

我喜欢使用它们的方式是在视图控制器类中为每个 uibarbuttonitem 创建一堆 IBOutlets。在 Interface Builder 中,我将第一个(默认)项目放在工具栏上,通常作为工具栏子视图和所有这些,其余的我放在 xib 中的顶级项目(与连接到的视图相同的级别)视图控制器的 .view 属性出口)。这样我就不必以编程方式创建它们。并且仍然将它们隐藏起来,以便稍后将它们附加到 uitolbar。

如果您使用这种方法,如果您使用 ivar IBOutlets(与 @property IBOutlets 相对)作为 xib 中未连接到 KVC 的所有顶级对象,请不要忘记在顶级 IBOutlets 上调用 release合规参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多