iPhone UIButton buttonType always evaluates to 0?
Whenever I execute the code below I always get button type = 0 no matter what I specify for buttonWithType. Does anyone know an explanation for this behavior? I'm trying to distinguish button based on their type obviously without success since the buttonType property appears to be broken (or useless). As always thanks very much for any enlightenment you may provide!
UIButton *button = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];
button.frame = frame;
NSLog(@"###################### LeftCalloutButtons - buttonWithTitle开发者_开发百科(73) - button type = %d",button.buttonType);
I encountered cases when UIButton properties (backgroundColor in my case) would have bogus value until the button is added to a view. Try querying the property once the button is on the screen.
EDIT: just saw another example of this behavior. This time it was about UISwitch.on. If I set it to YES right after alloc/init, it's not displayed as on. If I set it to YES in the viewDidLoad handler, it works as expected.
Apparently there's some lazy loading going on in the UIKit.
I have just verified the same issue with iOS 4.3.2 and Xcode 4.0.2. When looking at the internal structures of the UIButton
instance in the debugger, the private struct _buttonFlags
contains the unsigned int buttonType
which is set to the correct value (non-zero in my case).
However, the debugger reports in response to print (int) [control buttonType]
always 0
.
In conclusion the method buttonType
is broken and I will report this as a bug to Apple. I think it won't harm if other people who tried to use this method do the same.
精彩评论