切换坏了,语法错误?

时间:2015-07-13 15:50:52

标签: ios switch-statement

由于某种原因,此switch语句不希望识别我的视图控制器类名称和附加变量(IndexViewController遵守indexVC)。如果我在常规if或without依赖语句中执行它,它确实识别相同的代码段。我实际上查了一下switch的语法,因为我觉得我的思绪已经泄露了一些记忆。但语法似乎没问题。任何人都可以指出我的错误吗?感谢

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

switch (indexPath.row) {
    case 0:
        IndexViewController *indexVC = [self.storyboard instantiateViewControllerWithIdentifier:@"IndexViewController"];
        [self.currentVC presentViewController:indexVC animated:YES completion:nil];
        break;
    case 1:
        ProtectionViewController *proVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ProViewController"];
        [self.currentVC presentViewController:proVC animated:YES completion:nil];
        break;
    case 2:
        UsersViewController *usersVC = [self.storyboard instantiateViewControllerWithIdentifier:@"UsersViewController"];
        [self.currentVC presentViewController:usersVC animated:YES completion:nil];
        break;
    case 3:
        StatisticsViewController *statisticsVC = [self.storyboard instantiateViewControllerWithIdentifier:@"StatisticsViewController"];
        [self.currentVC presentViewController:statisticsVC animated:YES completion:nil];
        break;

    default:
        NSLog(@"We dont beleive in defaults");
        break;
}

}

1 个答案:

答案 0 :(得分:2)

您需要提供案例局部变量范围:

switch (indexPath.row) {
    case 0: {
        IndexViewController *indexVC = [self.storyboard instantiateViewControllerWithIdentifier:@"IndexViewController"];
        [self.currentVC presentViewController:indexVC animated:YES completion:nil];
        break;
    }
    case 1: {
        ...
    }
    ...

添加大括号。