2012-06-21 81 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"]; 
相關問題