Iphone – How to change the color of a UIWindow from a separate class

background-colorclassiphoneuiwindow

I am trying to change the background color of a UIWindow when an action is sent to the class MyController. However the UIWindow resides in the AppDelegate class so I don't have access to the variable to modify it using

window.backgroundColor = [UIColor theColor];

in MyController. Here is the code for MyController.m:

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

And here is the code for AppDelegate.h:

@interface AppDelegate : NSObject  {
    UIWindow *window;
}

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

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

@end

I tried to implement a method changeColorToRed… in the AppDelegate class because that method has access to the variable window, but I had no way to call that from the sliderChanged method in MyController.

How can I modify the UIWindow * window from another class?

Best Answer

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

If there's only 1 window,

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