1

3つのボタンを持つFirstControllerという名前のViewControllerがあり、それらのボタンの1つに触れるたびに、SecondController(他のViewController)が開きます。ただし、3つのボタンすべてが同じViewControllerを開く場合でも、ViewControllerが完全に同じになることは望ましくありませんが、押されたボタンに応じて、オブジェクトが異なります。SecondControllerにScrollViewがあり、どのボタンが押されたかに応じて、サブビューとして別の画像をScrollViewに追加したいと思います。

これが私がこれまでに得たものです:


#import "FirstController.h"
#import "SecondController.h"

@interface Level1 ()

@end

@implementation FirstController

- (IBAction) button1 {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SecondController *ViewForButton1 = [mainStoryboard instantiateViewControllerWithIdentifier:@"View2"];
}
- (IBAction) button2 {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SecondController *ViewForButton2 = [mainStoryboard instantiateViewControllerWithIdentifier:@"View2"];
}
- (IBAction) button3 {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SecondController *ViewForButton3 = [mainStoryboard instantiateViewControllerWithIdentifier:@"View2"];
}

@end

ホールビューのサブビューとして画像を追加する方法は知っていますが、ScrollViewに含める必要があります。ScrollViewをこのクラスに実装し、サブビューを追加するにはどうすればよいですか?

PS:ボタンは3つ以上ありますが、この例では3つしか使用していません。

4

3 に答える 3

0

Tagそれぞれに渡してUIButton、ボタンタップ時にタグを取得し、このタグを渡してYourSecondViewController、タップされたボタンに基づいて表示したい画像の条件を置きます。

于 2013-01-26T16:58:17.493 に答える
0

あなたが書いたように、あなたの mainStoryboard オブジェクトはすべてインスタンス化され、それらが作成された個々のメソッド内でのみスコープを持ちます。同じことが ViewForButton_ オブジェクトにも当てはまります。それらが異なる名前を持っているという事実は無関係です。

これは、その事実だけで、それらを互いに異なるオブジェクトにします。同じクラスの他のオブジェクトとは異なる独自の内部状態を持つことができます。

アップデート

各ボタンメソッドでこれを試してください:

- (IBAction) button1 {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SecondController *ViewForButton1 = [mainStoryboard instantiateViewControllerWithIdentifier:@"View2"];
... create views to add to the second view controller here
... add he views that you created to the second view controller

}

ある時点で、2 番目のビュー コントローラーに関連付けられたビューを表示したいと思うでしょう。それはあなたに任せますが、同じ方法でそれを行うと思います。

ちなみに、これは AppDelegate 自体とは何の関係もありません。

于 2013-01-26T17:02:32.073 に答える
0

これを行う別の方法として提案します。スクロール ビューに異なるビューを配置するには、SecondController 内の viewDidLoad メソッドで行う必要があります。スクロール ビューにアイテムを追加するには、そのスクロール ビューに IBOutlet が必要です。これは、最初にコントローラーを FirstController からインスタンス化するときにはまだ設定されていません。したがって、ボタン メソッドを 1 つだけ用意し、それを使用して SecondController をインスタンス化し、押されたボタンのタグに依存するプロパティ (この例では buttonTag と呼ばれます) を設定します。

-(IBAction)goToSecondController:(UIButton *)sender {
    SecondController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"Next"];
    second.buttonTag = sender.tag;
    [self.navigationController pushViewController:second animated:YES];
}

次に、SecondController で、switch ステートメントでそのプロパティを使用して、必要なものを追加します。

- (void)viewDidLoad {
    [super viewDidLoad];

    switch (self.buttonTag) {
        case 1:
            [self.scrollView addsubview:someView];
            break;
        case 2:
            [self.scrollView addsubview:someOtherView];
            break;
        case 3:
            [self.scrollView addsubview:anotherView];
            break;
        default:
            break;
    }
}
于 2013-01-26T17:48:09.603 に答える