-
28-09-2019 - |
题
假设您有2个 UIButton
的,纽扣和纽扣。如果您想致电 FlipsideViewController
从这两个按钮中,唯一的区别将是背景图像。 (即:如果按下按钮,则背景会出现在 FlipsideViewController
的视图,否则,它将是背景。)
现在,默认设置了第一个背景(背景)。如果按下按钮,我如何处理第二个背景图像(背景b)?
解决方案
根据您的介绍方式,有几种方法是:
- 使“背景”成为flipsideViewController的属性,并在显示VC之前根据每个按钮的操作方法将其设置。
- 在flipsideViewController中添加一个自定义初始方法,并带有“背景”参数。
“背景”可以是int或enum属性/参数,然后FlipsideViewController中的代码将根据该值竭尽所能。
编辑:
使用属性方法:
首先,在FlipsideViewController中,请确保您有一个名为SAD的UIImageView的IBOUTLET 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];
}
不隶属于 StackOverflow