Ven*_*ani 0 cocoa-touch objective-c ios uibackgroundcolor
我已经viewDidLoad:在我的tableview控制器中尝试过了,但是只更改了那个屏幕,我需要更改我的所有屏幕.
self.myTableView.backgroundColor = [UIColor purpleColor];
myTableView.opaque = NO;
Run Code Online (Sandbox Code Playgroud)
1.
SettingsBaseViewController并让所有ViewControllers继承自此BaseViewController.viewDidLoad:或viewWillAppear:你的BaseViewController.例如:
//Settings.h
@interface Settings : NSObject
+ (Settings *)sharedSettings;
@property(nonatomic, strong) UIColor *themeColor
@end
//Settings.m
+(Settings *)sharedSettings {
static Settings *sharedSettings = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSettings = [[self alloc] init];
});
return sharedSettings;
}
//BaseViewController.m
#import "Settings.h"
...
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[Settings sharedSettings].themeColor];
}
Run Code Online (Sandbox Code Playgroud)
2.
还有另一种方法可以实现这一点,即通知设计模式
每当您更改设置中的颜色时,都会发送通知并在您的通知中接收该通知BaseViewController.
//Settings.m
- (void)colorChanged:(UIColor *)color {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ThemeColorChanged" object:color];
}
//BaseViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(@"notificationThemeColorChanged:") name:@"ThemeColorChanged" object:nil];
}
- (void)notificationThemeColorChanged:(NSNotification *)notification {
UIColor *changedColor = notification.object;
[self.view setBackgroundColor:changedColor];
}
Run Code Online (Sandbox Code Playgroud)
为了节省
如果您需要保存数据,以便每当用户返回应用程序时,他会看到上次更新的颜色,然后您可以将其保存到UserDefaults.
//Settings.h
@interface Settings : NSObject
+ (Settings *)sharedSettings;
@property(nonatomic, strong) UIColor *themeColor
@end
//Settings.m
+(Settings *)sharedSettings {
static Settings *sharedSettings = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSettings = [[self alloc] init];
});
return sharedSettings;
}
- (instancetype)init {
if(self = [super init]) {
UIColor *savedColor = [[NSUserDefaults standardUserDefaults] objectForKey:@"ThemeColor"];
if(savedColor) {
self.themeColor = savedColor;
} else {
self.themeColor = defaultColor; //Ex:[UIColor whiteColor];
}
}
return self;
}
- (void)colorChanged:(UIColor *)color {
[[NSUserDefaults standardUserDefaults] setObject:color forKey:@"ThemeColor"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Run Code Online (Sandbox Code Playgroud)