为什么这段代码不起作用?

时间:2010-10-04 00:47:04

标签: objective-c

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

 if ([indexPath section] == 0) {
  switch ([indexPath row]) {

   case 0:
    [self renameExercise];
    [[self tableView] deselectRowAtIndexPath:indexPath 
            animated:YES];
    break;

   case 1:
    EditRootNoteViewController *newController = [[EditRootNoteViewController alloc] initWithNibName:@"EditNoteView"
                           bundle:nil];
    [newController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [newController setDelegate:self];
    [self presentModalViewController:newController
          animated:YES];
    [newController release];
    break;
xCode'需要在EditRootNoteViewController之前进行表达式......但为什么要来?这个相同的代码在这个开关之外工作......这可能是某种线索,但我没有丝毫之处。

3 个答案:

答案 0 :(得分:2)

尝试将代码放在一个块中:

...
  switch ([indexPath row]) {

   case 0:
    [self renameExercise];
    [[self tableView] deselectRowAtIndexPath:indexPath 
            animated:YES];
    break;

   case 1: {
    EditRootNoteViewController *newController = [[EditRootNoteViewController alloc] initWithNibName:@"EditNoteView" bundle:nil];
    [newController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [newController setDelegate:self];
    [self presentModalViewController:newController animated:YES];
    [newController release];
    } break;
  }

或者更好的是,将该代码提取到一个单独的方法中。

答案 1 :(得分:2)

这是因为你不能将变量声明为switch语句中case的第一个语句。

有关详细信息,请参阅this questionthis question

答案 2 :(得分:1)

这是你的整个开关声明吗?如果是这样,你就忘记了

default:
break;

部分。

确保您的问题包含完整方法,或至少包括完整方法,以便我们更轻松地提供帮助。

编辑: 哦!在第二次查看之后,如果在switch语句中声明新变量,则必须在大括号内执行此操作。不确定为什么,几个星期前我遇到了这个问题。也许有人可以详细说明为什么需要它?

相关问题