2012-06-21 52 views
0

我已在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

回答

2

让你的UIImageView,imageBg属性,综合它。

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

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

简单!只需将imageBg作为AppDelegate中的本地属性和实例即可。不要忘记综合你的属性。而在RootViewController的类将此代码放在一个按钮IBAction连接到UIButton

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

你可以做的另一种方法,这是在应用程序委托添加一个方法:

- (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!

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"]; 
相关问题