1

buttonWithTypeこのメソッドを使用して、特定のタイプのUIButtonを取得できることはわかっています。

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

今日、私はUIButtonのalloc/initメソッドを何気なく使用してUIButtonを作成します。

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

その後、ボタンのタイプを変更する方法がないことがわかりました。UIButtonのbuttonTypeプロパティは読み取り専用であるためです。

UIKit.frameworkでは、UIButton.h:

@property(nonatomic,readonly) UIButtonType buttonType;

私の質問は、alloc / initを使用してUIButtonを作成する場合、UIButtonのタイプを変更できますか?そうでない場合は、 UIButtonのようなinitメソッドがないため、このメソッドを使用して自動リリースUIButtonを取得することしかできません。少し奇妙に聞こえますが、initWithTypeメソッドを提供してみませんか?-initWithType+buttonWithType

4

1 に答える 1

4

+ buttonWithTypeがそれを処理するのに、なぜ割り当てて初期化する必要があるのでしょうか。このようにあなたの考えを逆にしてください:

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