如何更改背景图片?

发布于 2024-09-29 23:15:18 字数 267 浏览 5 评论 0原文

假设在一个应用程序中您有 2 个 UIButton,即buttonA 和buttonB。如果您想从这两个按钮调用FlipsideViewController,唯一的区别是背景图像。 (即:如果按下buttonA,BackGroundA将出现在FlipsideViewController的视图中,否则,它将出现BackGroundB。)

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

Assume in an app you have 2 UIButton's, buttonA and buttonB. If you want to call the FlipsideViewController from these 2 buttons, where the only the difference will be the background images. (i.e.: if buttonA is pressed, BackGroundA will appear in the FlipsideViewController's view, otherwise, it will be BackGroundB.)

Now the First BackGround (BackGroundA) is set by default. How do I handle the second background image (BackGroundB) if buttonB is pressed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强者自强 2024-10-06 23:15:18

根据您呈现 FlipsideViewController 的方式,有以下几种方法:

  • 将“background”设为 FlipsideViewController 的属性,并在显示 vc 之前根据需要在每个按钮的操作方法中设置它。
  • 在 FlipsideViewController 中添加一个带有“background”参数的自定义 init 方法。

“background”可以是一个 int 或 enum 属性/参数,然后 FlipsideViewController 中的代码将根据该值执行其自身需要的任何操作。

编辑:
要使用属性方法:

首先,在 FlipsideViewController 中,确保 UIImageView 有一个名为 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];
}

Depending on how you are presenting FlipsideViewController, a couple of ways are:

  • Make "background" a property of FlipsideViewController and set it as needed in each button's action method before showing the vc.
  • Add a custom init method in FlipsideViewController with a "background" parameter.

"background" could be an int or enum property/parameter and then the code in FlipsideViewController will do whatever it needs to itself based on that value.

Edit:
To use the property approach:

First, in FlipsideViewController, make sure you have an IBOutlet for the UIImageView called say backgroundImageView.

Next, in FlipsideViewController.h, add a property to set the background (I'm using an int):

@interface FlipSideViewController : UIViewController {
    int backgroundId;
}
@property (assign) int backgroundId;

Next, in FlipsideViewController.m, add this:

@synthesize backgroundId;

-(void)viewWillAppear:(BOOL)animated
{
    if (backgroundId == 2)
        self.backgroundImageView.image = [UIImage imageNamed:@"background2.png"];
    else
        self.backgroundImageView.image = [UIImage imageNamed:@"background1.png"];
}

Finally, in the main view controller, the button action method would look something like this:

-(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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文