UIViewController:使用未声明的标识符

时间:2011-05-07 13:40:24

标签: iphone objective-c uiviewcontroller

我有一个看起来像这样的方法:

- (void)testMethod:(int)myNumber {
    switch (myNumber) {
        case 0: {
            MyViewController0 *controller = [self.myViewControllers objectAtIndex:myNumber];
            if ((NSNull *)controller == [NSNull null]) {
                controller = [[MyViewController0 alloc] initWithNibName:@"MyView0" bundle:nil];
                controller.aVariable = self.myVariable;
                [self.myViewControllers replaceObjectAtIndex:myNumber withObject:controller];
                [controller release];
            }
            if (controller.view.superview == nil) {
                // do some stuff with controller ...
            }
            break;
        }
        case 1: {
            MyViewController1 *controller = [self.myViewControllers objectAtIndex:myNumber];
            if ((NSNull *)controller == [NSNull null]) {
                controller = [[MyViewController1 alloc] initWithNibName:@"MyView1" bundle:nil];
                [self.myViewControllers replaceObjectAtIndex:myNumber withObject:controller];
                [controller release];
            }
            if (controller.view.superview == nil) {
                // do some stuff with controller ...
            }
            break;
        }
        // ...
    }
}

我的问题涉及这些行,它们总是一样的:

if (controller.view.superview == nil) {
    // do some stuff with controller ...
}

所以,我想在switch-case中删除这些行并将它们放在方法的末尾。

问题是,如果我这样做,我会收到错误:Use of undeclared identifier controller。 我认为这是因为controller可能仍未声明(如果没有成功的话)。

但是我可以做些什么来避免将这些行放在每个case语句中,我可以在方法结束时把它放一次吗?

2 个答案:

答案 0 :(得分:1)

这是因为你只在那些特定范围内声明变量,并且你试图在它声明的范围之外使用它(你不能,这是范围的整个点)。

在切换之前需要这个:

UIViewController* controller = nil;

然后在您的case语句中,您只需分配给现有的controller变量,而不是在该范围内声明它。

答案 1 :(得分:1)

- (void)testMethod:(int)myNumber {
    UIViewController* controller = nil;

    switch (myNumber) {
        case 0: {
            controller = [self.myViewControllers objectAtIndex:myNumber];
            if ((NSNull *)controller == [NSNull null]) {
                controller = [[MyViewController0 alloc] initWithNibName:@"MyView0" bundle:nil];
                ((MyViewController0*)controller).aVariable = self.myVariable;
                [self.myViewControllers replaceObjectAtIndex:myNumber withObject:controller];
                [controller release];
            }

            break;
        }
        case 1: {
            controller = [self.myViewControllers objectAtIndex:myNumber];
            if ((NSNull *)controller == [NSNull null]) {
                controller = [[MyViewController1 alloc] initWithNibName:@"MyView1" bundle:nil];
                [self.myViewControllers replaceObjectAtIndex:myNumber withObject:controller];
                [controller release];
            }
            break;
        }
        // ...
    }

    if( !controller ) {
       //todo
    } else
    if ( !controller.view.superview ) {
                // do some stuff with controller ...
    }


}