我有一个tableview,在第一行中,我试图最初向detailtextlabel添加一个静态值。因此,当最初加载tableview时,detailtextlabel显示默认值。当我点击detailtextlabel时,它会切换。我添加了将默认值切换为另一个值的代码。这工作正常。 这是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
cell = [tableView cellForRowAtIndexPath:indexPath];
switch (indexPath.row) {
case 0:
if ([cell.detailTextLabel.text isEqualToString:@"digital"]) {
cell.detailTextLabel.text = @"analog";
userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:@"analog" forKey:@"clock"];
}
else {
cell.detailTextLabel.text = @"digital";
[userdefaults setObject:@"digital" forKey:@"clock"];
}
[userdefaults synchronize];
break;
default:
break;
}
}
但我的问题是当标签从数字变为模拟时,数字视图控制器(作为主窗口中的第一个标签添加)应该更改为模拟视图控制器,当模拟更改为数字时,数字视图控制器应由第一个选项卡中的模拟视图控制器替换。
//在appdidfinish启动时我已完成此代码,但它不会在运行时更改标签栏。
NSString *clockswitch = [[NSUserDefaults standardUserDefaults]objectForKey:@"clock"];
if ([clockswitch isEqualToString:@"digital"]) {
DigitalClockViewController *dig = [[DigitalClockViewController alloc]init];
//self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
}
else
{
AnalogClockViewController *ana = [[AnalogClockViewController alloc]init];
//self.tabBarController.selectedViewController = [self.tabBarController.ana objectAtIndex:0];
}
答案 0 :(得分:0)
UITabBarController.viewControllers is an immutable array you need to make a mutable copy.
NSString *clockswitch = [[NSUserDefaults standardUserDefaults]objectForKey:@"clock"];
UIViewController *desiredController = nil;
if ([clockswitch isEqualToString:@"digital"]) {
desiredController = [[DigitalClockViewController alloc]init];
}
else {
desiredController = [[AnalogClockViewController alloc]init];
}
NSMutableArray *controllersCopy = [self.tabBarController.viewControllers mutableCopy];
[controllersCopy replaceObjectAtIndex:0 withObject:desiredController];
self.tabBarController.viewControllers = controllersCopy;