-1

私は現在、オブジェクトを動的に作成し、それらをViewControllerに表示することを要求するアプリを設計しています。私の問題は、必要なオブジェクトを作成するためにドキュメントを調べていくことです。私はいくつかの検索を行い、ただ座ってそれを理解しようとさえしましたが、残念ながら、私はそれを示すものが何もありません。ViewControllerを動的に作成する方法はすでに知っていますが、オブジェクトを取得できないようです。

XCodeのObjective-Cを使用してオブジェクト(ボタンとスライダーを言う)を動的に作成する方法を知っている人はいますか?

4

1 に答える 1

1

コードでボタンを作成するのは簡単です

- (void)addButton {

    // Create button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    // Set the frame (location and size)
    [button setFrame:CGRectMake(0, 0, 40, 30)];

    // Set what happens when you touch the button
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

    // Set button text
    [button.titleLabel setText:@"Button"];

    // Add button to view
    [self.view addSubview:button];

}

多くのインターフェイス要素は、同様のパターンに従ってオブジェクトを初期化し、フレームを設定してビューに追加します。

于 2012-06-21T23:19:16.550 に答える