更改整个应用背景

时间:2012-06-21 12:46:28

标签: iphone ipad

我在 applicationDidFinishLaunching 中进行了一次imageview,如下所示。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      UIImageView* imageBg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 320, 460)];
      imageBg.image = [UIImage imageNamed:@"AppBG.png"];
       [self.window addSubview:imageBg];
       [self.window sendSubviewToBack:imageBg]; 
 }

现在在 rootViewController 我有一个按钮

我需要在按下按钮 RootViewController 中我想将图像从AppBG.png更改为AppBG1.png

3 个答案:

答案 0 :(得分:3)

//add a tag to your imageview
imageBg.tag = 1001;

//fetch the imageview from window like this
UIImageView *imgView = [self.window viewWithTag:1001];

//use this imageView to replace existing image like this
imageView.image = [UIImage imageNamed:@"newimg.png"];

答案 1 :(得分:2)

轻松!只需在AppDelegate中创建imageBg本地属性和实例即可。不要忘记合成你的属性。在RootViewController类中,将此代码放在一个连接到IBAction的按钮UIButton中:

- (IBAction)buttonWasPressed {
AppDelegate *delegate = [[AppDelegate alloc] init];
delegate.imageBg.image = [UIImage imageNamed: @"AppBG1.png"];
// Don't forget memory management!
[delegate release];
}

您可以执行此操作的另一种方法是在app delegate中添加一个方法:

- (void)changeImage {

 self.imageBg.image = [UIImage imageNamed: @"AppBG1.png"];
}

在RootViewController中调用此方法:

- (IBAction)buttonWasPressed {
AppDelegate *delegate = [[AppDelegate alloc] init];
[delegate changeImage];
// Don't forget memory management!
[delegate release];
}

简单的Objective-C!

答案 2 :(得分:2)

让您的UIImageViewimageBg属性合成。

然后在按钮点击上使用以下代码:

MyAppdelegate *appdelegate = (MyAppdelegate *)[[UIApplication sharedApplication] delegate];
appdelegate.imageBg.image = [UIImage imageNamed:@"AppBG1.png"];