1

假设在一个应用程序中你有 2 个UIButton,buttonA 和 buttonB。如果你想FlipsideViewController从这两个按钮中调用,唯一的区别就是背景图像。(即:如果 buttonA 被按下,BackGroundA 将出现在FlipsideViewController's 的视图中,否则,它将是 BackGroundB。)

现在默认设置第一个背景(BackGroundA)。如果按下按钮B,我如何处理第二个背景图像(BackGroundB)?

4

1 回答 1

3

根据您呈现 FlipsideViewController 的方式,有几种方法:

  • 将“背景”设置为 FlipsideViewController 的一个属性,并在显示 vc 之前在每个按钮的操作方法中根据需要进行设置。
  • 在 FlipsideViewController 中添加一个带有“背景”参数的自定义 init 方法。

“背景”可以是一个 int 或 enum 属性/参数,然后 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 回答