import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
- (void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
if ([[user objectForKey:@”day”] isEqualToString:@”yes”]) {
self.view.backgroundColor = [UIColor whiteColor];
} else {
self.view.backgroundColor = [UIColor blackColor];
}
}
// 非通知实现夜间模式
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @”view”;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:button];
[button setTitle:@”change” forState:UIControlStateNormal];
button.frame = CGRectMake(100, 200, 60, 30);
[button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)button:(UIButton *)button
{
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
if ([[user objectForKey:@”day”] isEqualToString:@”yes”]) {
button.selected = 0;
} else {
button.selected = 1;
}
button.selected = !button.selected;
if (button.selected == 0) {
[user setObject:@”yes” forKey:@”day”];
self.view.backgroundColor = [UIColor whiteColor];
[self.view setNeedsDisplay];
} else {
[user setObject:@"no" forKey:@"day"];
self.view.backgroundColor = [UIColor blackColor];
[self.view setNeedsDisplay];
}
}
//接下来每个vc这样写就行了
import “View3Controller.h”
@interface View3Controller ()
@end
@implementation View3Controller
- (void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
if ([[user objectForKey:@”day”] isEqualToString:@”yes”]) {
self.view.backgroundColor = [UIColor whiteColor];
} else {
self.view.backgroundColor = [UIColor blackColor];
}
}