2010-01-09 45 views
3

我试图改变一个UIWindow的行为发送到类MyController的背景颜色。然而,一个UIWindow驻留在AppDelegate类,所以我不能够访问该变量在使用myController的如何从单独的类中更改UIWindow的颜色?

window.backgroundColor = [UIColor theColor]; 

对其进行修改。下面是MyController.m代码:

@implementation MyController 
- (IBAction)sliderChanged:(id)sender { 
//want to call some method to change the UIWindow background color 
} 
@end

这里是AppDelegate.h代码:在AppDelegate类因为该方法

 
@interface AppDelegate : NSObject { 
    UIWindow *window; 
} 

- (void)changeColorToRed:(int)r Green:(int)g Blue:(int)b; 

@property (nonatomic, retain) IBOutlet UIWindow *window; 

@end 

我试图执行一个方法changeColorToRed ...有权访问变量window,但我无法从MyController中的sliderChanged方法调用该变量。

如何从另一个类修改UIWindow *窗口?

回答

9
[UIApplication sharedApplication].delegate.window.backgroundColor = [UIColor myColor]; 

如果只有1个窗口,

[UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor myColor]; 
0

我想你既可以:

做一些静态方法包含窗口变量

类将窗口变量传递给新类。