簡體   English   中英

如何更改背景圖片?

[英]How to change the background image?

假設在一個應用程序中,您有2個UIButton ,buttonA和buttonB。 如果要從這2個按鈕調用FlipsideViewController ,則唯一的區別就是背景圖像。 (即:如果按下buttonA,則BackGroundA將出現在FlipsideViewController的視圖中,否則它將是BackGroundB。)

現在,默認設置了第一個BackGround(BackGroundA)。 如果按下buttonB,如何處理第二個背景圖像(BackGroundB)?

根據呈現FlipsideViewController的方式,有兩種方法:

  • 將“ background”設置為FlipsideViewController的屬性,並在顯示vc之前根據需要在每個按鈕的action方法中進行設置。
  • 在FlipsideViewController中使用“ background”參數添加自定義init方法。

“ background”可以是int或enum屬性/參數,然后FlipsideViewController中的代碼將根據該值對其自身執行所需的任何操作。

編輯:
要使用屬性方法:

首先,在FlipsideViewController中,確保為UIImageView擁有一個名為say backgroundImageView的IBOutlet。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM