2010-11-02 97 views
1

假设你的应用程序中有2个UIButton,按钮A和按钮B.如果您想从这两个按钮拨打FlipsideViewController,其中唯一的区别将是背景图像。 (即:如果按钮A被按下,BackGroundA将出现在FlipsideViewController的视图中,否则它将成为BackGroundB。)如何更改背景图片?

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

回答

3

取决于你如何呈现FlipsideViewController,一对夫妇的方法是:

  • 使“背景” FlipsideViewController的属性,将其设置为需要的每个按钮的操作方法显示了VC之前。
  • 在FlipsideViewController中添加一个具有“background”参数的自定义init方法。

“背景”可以是一个int或枚举属性/参数,然后FlipsideViewController中的代码将根据该值做它自己需要的任何内容。

编辑:
使用属性的方法:

首先,在FlipsideViewController,确保你有一个IBOutlet的叫的UIImageView说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]; 
} 
+0

我试图声明UIImage但它e是没有方法像“setBackground”或类似的东西有没有特定的代码? – 2010-11-02 16:08:12

+0

默认情况下,“第一背景”是如何设置的? – Anna 2010-11-02 16:12:31

+0

我使用了界面生成器的图像视图并将其设置回 – 2010-11-02 16:25:00