【发布时间】:2014-07-04 09:32:34
【问题描述】:
我的一个应用程序中有一个非常简单的工具栏按钮,允许用户上传对象。在编辑对象时,我隐藏工具栏(编辑时不允许上传)。我还想根据对象是否有任何尚未上传的更改来启用/禁用上传按钮。 (例如,上传成功后上传按钮被禁用,编辑后上传按钮被启用)
我有一个像这样的 uploadButton 属性
@property UIBarButtonItem *uploadButton;
并像这样在 viewDidLoad 中设置它:
self.uploadButton = [[UIBarButtonItem alloc] initWithTitle:@"Upload" style:UIBarButtonItemStyleDone target:self action:@selector(upload:)];
在上传操作期间,我使用工具栏显示上传进度条,但在此之前和上传之后,我将上传按钮添加到工具栏,如下所示:
NSArray *toolbarItems = @[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL],
self.uploadButton,
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL]];
[self setToolbarItems:toolbarItems animated:YES];
但是,有时设置self. self.uploadButton.enabled 属性似乎没有效果(按钮的外观没有改变)。在按钮应该启用但显示为禁用的情况下,我检查了调试器,发现self.uploadButton.enabled 实际上设置正确,甚至在实际更改事件之后是YES。
然后,我深入研究了视图层次结构,发现了一些奇怪的地方:
(lldb) po [[0xf72eba0 uploadButton] view]
nil
(指针指向视图控制器对象,因此它与上面的“self”相同)
另外:[0xf72eba0 uploadButton] 不是零,所以这不是消息nil 的情况。
并且:是的 -view 不是 UIBarButtonItem 上记录的方法,但由于 UIBarButtonItem 本身不是 UIView,我认为它必须有一个视图,显然我是对的。
为了说明这一点,以下
(lldb) po [[0xf72eba0 editButtonItem] view]
<UINavigationButton: 0xf782de0; frame = (645 8; 42 30); opaque = NO; layer = <CALayer: 0xf784500>>
有观点。同样在随后的运行中,当一切都按预期工作时,上传按钮也有一个视图:
(lldb) po [[0xf72eba0 uploadButton] view]
<UIToolbarTextButton: 0x1196eb40; frame = (323 0; 57 44); opaque = NO; layer = <CALayer: 0x1196f330>>
总结一下:
- uploadButton 没有释放,它是一个强大的属性。而且,当我检查时它仍然存在。
- enabled 属性设置正确。
- 奇怪的是,有时uploadButton 似乎没有-view。这似乎与未正确更新有关。
【问题讨论】:
标签: ios objective-c uibarbuttonitem uitoolbar