1

アプリUIButtonに buttonA と buttonB の 2 つがあるとします。これら 2 つのボタンからを呼び出したい場合FlipsideViewController、唯一の違いは背景画像です。(つまり、buttonA が押された場合、BackGroundA がFlipsideViewControllerのビューに表示されます。それ以外の場合は、BackGroundB になります。)

現在、最初の背景 (BackGroundA) がデフォルトで設定されています。buttonB が押された場合、2 番目の背景画像 (BackGroundB) を処理するにはどうすればよいですか?

4

1 に答える 1

3

FlipsideViewController の提示方法に応じて、いくつかの方法があります。

  • "background" を FlipsideViewController のプロパティにし、VC を表示する前に各ボタンのアクション メソッドで必要に応じて設定します。
  • FlipsideViewController に「background」パラメーターを指定してカスタム init メソッドを追加します。

「背景」は int または列挙型のプロパティ/パラメータである可能性があり、FlipsideViewController のコードは、その値に基づいて必要なことを実行します。

編集:
プロパティアプローチを使用するには:

まず、FlipsideViewController で、say という UIImageView の IBOutlet があることを確認しますbackgroundImageView

次に、FlipsideViewController.h で、背景を設定するプロパティを追加します (私は int を使用しています)。

@interface FlipSideViewController : UIViewController {
    int backgroundId;
}
@property (assign) int backgroundId;

次に、FlipsideViewController.m に以下を追加します。

@synthesize backgroundId;

-(void)viewWillAppear:(BOOL)animated
{
    if (backgroundId == 2)
        self.backgroundImageView.image = [UIImage imageNamed:@"background2.png"];
    else
        self.backgroundImageView.image = [UIImage imageNamed:@"background1.png"];
}

最後に、メイン ビュー コントローラーでは、ボタン アクション メソッドは次のようになります。

-(IBAction)buttonPressed:(UIButton *)sender
{
    FlipSideViewController *fsvc = [[FlipSideViewController alloc] initWithNibName:nil bundle:nil];
    fsvc.backgroundId = sender.tag;  //assuming btn1.tag=1 and bnt2.tag=2
    [self presentModalViewController:fsvc animated:YES];
    [fsvc release];
}
于 2010-11-02T15:39:12.360 に答える