iOS switch语句中为什么不能直接进行页面跳转
没有复杂的逻辑 ,直接上代码。
switch (indexPath.row) {
case 0:
MyAccountViewController *myAccountVC = [[MyAccountViewController alloc]init];
[self.navigationController pushViewController:myAccountVC animated:YES];
break;
case 1:
MyorderViewController *MyorderVC = [[MyorderViewController alloc]init];
[self.navigationController pushViewController:MyorderVC animated:YES];
NSLog(@"服务订单");
break;
case 2:
NSLog(@"我的商品");
MyGoodsViewController *goodsViewController = [[MyGoodsViewController alloc]init];
[self.navigationController pushViewController:goodsViewController animated:YES];
break;
default:
break;
}
上面的代码Xcode会报错 ,解决方法是:在需要跳转的语句两端加上 {},如下引用部分。
switch (indexPath.row) {
case 0:
{
MyAccountViewController *myAccountVC = [[MyAccountViewController alloc]init];
[self.navigationController pushViewController:myAccountVC animated:YES];
}
break;
case 1:
{
MyorderViewController *MyorderVC = [[MyorderViewController alloc]init];
[self.navigationController pushViewController:MyorderVC animated:YES];
NSLog(@"服务订单");
}
break;
case 2:
{
NSLog(@"我的商品");
MyGoodsViewController *goodsViewController = [[MyGoodsViewController alloc]init];
[self.navigationController pushViewController:goodsViewController animated:YES];
}
break;
default:
break;
}