开发者

How can I change the color of a UIWindow from a separate class?

开发者 https://www.devze.com 2022-12-15 18:50 出处:网络
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

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开发者_JAVA技巧 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?


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

If there's only 1 window,

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


I think you could either:

Make some static method in the class containing the window variable

or

Pass the window variable to the new class.

0

精彩评论

暂无评论...
验证码 换一张
取 消