0

我在applicationDidFinishLaunching中拍摄了一个图像视图,如下所示。

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

4

3 回答 3

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"];
于 2012-06-21T12:53:31.390 回答
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!

于 2012-06-21T12:53:07.357 回答
2

使你的UIImageView,imageBg一个属性,合成它。

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

MyAppdelegate *appdelegate = (MyAppdelegate *)[[UIApplication sharedApplication] delegate];
appdelegate.imageBg.image = [UIImage imageNamed:@"AppBG1.png"];
于 2012-06-21T12:53:30.413 回答