![](/img/trans.png)
[英]how to change the background image of the cell in didSelectRowAtIndexPath
[英]How to change the background image?
假設在一個應用程序中,您有2個UIButton
,buttonA和buttonB。 如果要從這2個按鈕調用FlipsideViewController
,則唯一的區別就是背景圖像。 (即:如果按下buttonA,則BackGroundA將出現在FlipsideViewController
的視圖中,否則它將是BackGroundB。)
現在,默認設置了第一個BackGround(BackGroundA)。 如果按下buttonB,如何處理第二個背景圖像(BackGroundB)?
根據呈現FlipsideViewController的方式,有兩種方法:
“ 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.