2010-11-02 75 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