Iphone 如何更改背景图像?

Iphone 如何更改背景图像?,iphone,objective-c,cocoa-touch,sdk,pushviewcontroller,Iphone,Objective C,Cocoa Touch,Sdk,Pushviewcontroller,假设在一个应用程序中,你有2个UIButton,buttonA和buttonB。如果要从这两个按钮调用FlipsideViewController,其中唯一不同的是背景图像。(即:如果按下按钮A,则背景A将出现在FlipsideViewController的视图中,否则,背景B。) 现在默认设置第一个背景(BackGroundA)。如果按下按钮B,如何处理第二个背景图像(背景B)?根据您演示FlipsideViewController的方式,有以下几种方法: 在显示vc之前,将“背景”设置为F

假设在一个应用程序中,你有2个
UIButton
,buttonA和buttonB。如果要从这两个按钮调用
FlipsideViewController
,其中唯一不同的是背景图像。(即:如果按下按钮A,则背景A将出现在
FlipsideViewController
的视图中,否则,背景B。)


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

根据您演示FlipsideViewController的方式,有以下几种方法:

  • 在显示vc之前,将“背景”设置为FlipsideViewController的属性,并根据需要在每个按钮的操作方法中进行设置
  • 在FlipsideViewController中添加带有“background”参数的自定义init方法
“background”可以是int或enum属性/参数,然后FlipsideViewController中的代码将根据该值对自身执行任何需要的操作

编辑:
要使用属性方法,请执行以下操作:

首先,在FlipsideViewController中,确保UIImageView有一个名为say
backgroundImageView
的IBOutlet

接下来,在FlipsideViewController.h中,添加一个属性来设置背景(我使用的是int):

接下来,在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];
}

我试图声明UIImage,但没有像“setBackground”或类似的方法。是否有特定的代码?默认情况下“第一个背景”是如何设置的?我从界面生成器中使用了一个图像视图,并将其设置为backOk。您正在尝试哪种方法?属性或自定义初始化方法?fsvc.backgroundId=sender.tag;错误:请求非结构或联合中的成员“标记”
-(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];
}