为什么切换机箱不需要多行 如果做得好,
if(indexPath.row == 1){
UIViewController *viewController = [UIViewController new];
viewController.view.backgroundColor = [UIColor whiteColor];
viewController.title = _titlesArray[indexPath.row];
[self.navigationController pushViewController:viewControlleranimated:YES];
}
相同的代码无法在switch case语句中使用
switch (indexPath.row) {
case 0:
UIViewController *viewController = [UIViewController new];
viewController.view.backgroundColor = [UIColor whiteColor];
viewController.title = _titlesArray[indexPath.row];
[self.navigationController pushViewController:viewControlleranimated:YES];
break;
case 1:
break;
default
break:
}
在switch语句中,它给出了错误.... 我强行使用if else语句设置了switch case。 任何人都可以建议我如何使用开关案例的陈述。
答案 0 :(得分:2)
只要没有变量定义,开关就会占用多行。需要附上任何新的定义。所以,这些都是有效的:
case 0:
{
UIViewController *viewController = [UIViewController new];
viewController.view.backgroundColor = [UIColor whiteColor];
viewController.title = _titlesArray[indexPath.row];
[self.navigationController pushViewController:viewControlleranimated:YES];
break;
}
和
UIViewController *viewController;
...
case 0:
viewController = [UIViewController new];
viewController.view.backgroundColor = [UIColor whiteColor];
viewController.title = _titlesArray[indexPath.row];
[self.navigationController pushViewController:viewControlleranimated:YES];
break;