【发布时间】:2019-06-25 22:30:07
【问题描述】:
我正在尝试在我的应用程序中创建“更改主题”选项。出于某种原因,这段代码总是会以某种方式实现,这样当我点击单元格时,例如,如果我在我的应用程序当前为浅色主题时点击“深色主题”,我的所有代码都会触发并且导航栏变为深灰色,我的 tableview 背景变成了它应该是的颜色,等等,但是“Light Theme”保持在 Light 模式。除非我返回设置视图控制器并重新输入“ChangeThemeViewController”,否则它仍将保留白色背景和黑色文本。我很难过,我已经尝试了几个小时来解决这个问题。任何人都知道它为什么会这样以及我该如何解决?这是我的代码。
#import "ChangeThemeViewController.h"
@interface ChangeThemeViewController ()
@end
@implementation ChangeThemeViewController {
NSArray *tableData;
NSInteger theme;
NSString *simpleTableIdentifier;
UITableViewCell *cell;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSInteger theme = [[NSUserDefaults standardUserDefaults]
integerForKey:@"theme"];
[[NSUserDefaults standardUserDefaults] setInteger:theme forKey:@"theme"];
[[NSUserDefaults standardUserDefaults] synchronize];
//add variable for theme here
// if (theme == 0) {
// self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//
// self.navigationController.navigationBar.barTintColor = nil;
// _tableView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
// [self.navigationController.navigationBar
// setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
// }
//
//
// if (theme == 1) {
// self.tableView.backgroundColor = [UIColor blackColor];
//
// self.navigationController.navigationBar.barTintColor = [UIColor darkGrayColor];
// _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// [self.navigationController.navigationBar
// setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
// }
tableData = [NSArray arrayWithObjects:@"Light Theme", @"Dark Theme", nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
simpleTableIdentifier = @"SimpleTableItem";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
//add variable for theme here
NSInteger theme = [[NSUserDefaults standardUserDefaults]
integerForKey:@"theme"];
if (theme == 0) {
NSLog(@"theme is 0, running cellforrowindexpath");
cell.textLabel.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = nil;
tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
if (theme == 1) {
NSLog(@"theme is 1, running cellforrowindexpath");
UIColor *color = [UIColor colorWithRed:30/255.0
green:30/255.0
blue:30/255.0
alpha:1];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor blackColor];
tableView.backgroundColor = color;
}
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = [tableData objectAtIndex:0];
}
if (indexPath.row == 1) {
cell.textLabel.text = [tableData objectAtIndex:1];
}
}
}
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[NSUserDefaults standardUserDefaults] setInteger:theme forKey:@"theme"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSInteger theme = [[NSUserDefaults standardUserDefaults] integerForKey:@"theme"];
if (indexPath.row == 0) {
[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
NSLog(@"row 0 picked should change into light mode");
theme = 0;
[[NSUserDefaults standardUserDefaults] setInteger:theme forKey:@"theme"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.navigationController.navigationBar.barTintColor = nil;
_tableView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
self.navigationController.navigationBar.tintColor = nil;
}
if (indexPath.row == 1) {
[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
NSLog(@"row 1 picked should change into dark mode");
theme = 1;
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[[NSUserDefaults standardUserDefaults] setInteger:theme forKey:@"theme"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIColor *color = [UIColor colorWithRed:30/255.0
green:30/255.0
blue:30/255.0
alpha:1];
self.tableView.backgroundColor = color;
self.navigationController.navigationBar.barTintColor = [UIColor darkGrayColor];
_tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}
if (theme == 0) {
cell.backgroundColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor blackColor];
}
if (theme == 1) {
cell.backgroundColor = [UIColor blackColor];
cell.textLabel.textColor = [UIColor whiteColor];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
@end
【问题讨论】:
标签: ios objective-c xcode uitableview