1

Cocoaでプログラムでビューにコンポーネントを追加することは可能ですか?UIは、のInterfaceBuilderを使用して作成されXcodeます。いくつか追加したいのですNSButtonsが、その量と位置は実行時とユーザー入力でわかります。

誰かがそれが可能であり、それがどのように行われることができ、これらの動的コンポーネントをどのように配置するかを知っていますか?

4

1 に答える 1

1

もちろん可能です。サブビューの追加:

UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; // or UIButton, UIScrollView, or any other view-based class you make think of
[self addSubview:subview];

または、ボタンをより正確に言うと、次のようになります。

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(0, 0, 100, 100);
[aButton addTarget:self action:@selector(methodName) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:aButton]; // if done from the view self.view if done from the controller

ああ、申し訳ありませんが、iOSではなくOSXであることに気づきましたが、基本は同じです。代わりにNSButtonクラスを見てください。

NSButton *aButton = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; // x, y, width, height

始める必要があります。

于 2013-01-14T20:41:25.693 に答える