1

I know we can use the buttonWithType method to get an UIButton with specific type.

UIButton * button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];

Today, I casually use UIButton's alloc/init method to create an UIButton:

UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(20, 300, 100, 50)];

then I found there is no way to change the button's type.Because the UIButton's buttonType property is readonly.

In UIKit.framework, UIButton.h :

@property(nonatomic,readonly) UIButtonType buttonType;

My question is:if I use alloc/init to create a UIButton, can I change the UIButton's type? If not, since there is NOT any init method like -initWithType in UIButton, we can only use the +buttonWithType method to get an autorelease UIButton. It sounds a little strange, why not provide an initWithType method?

4

1 回答 1

4

如果 +buttonWithType 为您处理,为什么还要分配和初始化?像这样扭转你的想法:

UIButton * button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[button setFrame:CGRectMake(20, 300, 100, 50)];
于 2012-04-07T13:34:01.680 回答