iPhone UIButton buttonType总是评估为0?

时间:2010-01-03 02:55:09

标签: iphone iphone-sdk-3.0 properties uibutton

每当我执行下面的代码时,无论我为buttonWithType指定什么,我总是得到按钮类型= 0。有没有人知道这种行为的解释?我试图基于其类型区分按钮显然没有成功,因为buttonType属性似乎被破坏(或无用)。一如既往地感谢您提供的任何启示!

UIButton *button = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];
button.frame = frame;
NSLog(@"###################### LeftCalloutButtons - buttonWithTitle(73) - button type = %d",button.buttonType);

2 个答案:

答案 0 :(得分:0)

我遇到过UIButton属性(在我的情况下为backgroundColor)在将按钮添加到视图之前会出现虚假值的情况。一旦按钮出现在屏幕上,请尝试查询属性。

编辑:刚看到另一个这种行为的例子。这次是关于UISwitch.on。如果我在alloc / init之后立即将其设置为YES,则它不会显示为on。如果我在viewDidLoad处理程序中将其设置为YES,它将按预期工作。

显然UIKit正在进行一些延迟加载。

答案 1 :(得分:0)

我刚刚验证了iOS 4.3.2和Xcode 4.0.2的相同问题。查看调试器中UIButton实例的内部结构时,私有struct _buttonFlags包含设置为正确值的unsigned int buttonType(在我的情况下为非零)。

但是,调试器会响应print (int) [control buttonType]始终0报告。

总之,方法buttonType已被破解,我将此报告为Apple的错误。我认为如果试图使用这种方法的其他人也这样做也不会有害。