I have a tableview in which, in the first row, I am trying to initially add a static value to the detailtextlabel. So when the tableview is loaded initially, the detailtextlabel shows the default value. When I click on the detailtextlabel, it switches. I have added code that will switch the default value to another value. This is working properly.
This is my code:
- (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;
}
}
But my problem is when the label is getting changed from digital to analog, the digital view controller, which is added as the first tab in the mainwindow, should get changed to an analog viewcontroller and when analog is changed to digital, the digital viewcontroller should be replaced by the analog view controller in the first tab.
//In the appdidfinish launching I have done this code, but it does not change the tabbar at runtime.
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];
}
1 Answer