如何在使用alloc后初始化UIButton的类型?

时间:2012-04-07 13:25:50

标签: iphone objective-c ios uibutton

我知道我们可以使用buttonWithType方法获取具有特定类型的UIButton。

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

今天,我随意使用UIButton的alloc / init方法创建一个UIButton:

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

然后我发现没有办法改变按钮的类型。因为UIButton的buttonType属性是readonly。

在UIKit.framework中,UIButton.h:

@property(nonatomic,readonly) UIButtonType buttonType;

我的问题是:如果我使用alloc / init创建UIButton,我可以更改UIButton的类型吗?如果没有,因为在UIButton中有 NOT 任何初始化方法,例如-initWithType,我们只能使用+buttonWithType方法来获取自动释放UIButton。听起来有点奇怪,为什么不提供initWithType方法?

1 个答案:

答案 0 :(得分:5)

为什么甚至必须分配和init if + buttonWithType为你处理?只要改变你的想法:

UIButton * button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[button setFrame:CGRectMake(20, 300, 100, 50)];