简体   繁体   English

UIViewController:使用未声明的标识符

[英]UIViewController: Use of undeclared identifier

I have a method which looks like this: 我有一个看起来像这样的方法:

- (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;
        }
        // ...
    }
}

My problem involves these lines, which are always the same: 我的问题涉及这些行,它们总是一样的:

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

So, I want to remove these lines inside the switch-case and put them at the end of the method. 所以,我想在switch-case中删除这些行,并将它们放在方法的末尾。

The problem is, if I do this, I get the error: Use of undeclared identifier controller . 问题是,如果我这样做,我得到错误: Use of undeclared identifier controller I think it is because it is possible that the controller could remain undeclared (if no case is successful). 我认为这是因为controller可能保持未声明(如果没有成功的话)。

But what can I do to avoid putting these lines in every case statement, can I instead put it once at the end of the method? 但是我可以做些什么来避免在每个case语句中放入这些行,我可以在方法结束时把它放一次吗?

It's because you only declare the variable in those specific scopes, and you are trying to use it outside the scope it's declared (which you can't, that being the whole point of scope). 这是因为你只在那些特定的范围内声明变量,并且你试图在它声明的范围之外使用它(你不能,这是范围的整个点)。

You need this before your switch: 在切换之前需要这个:

UIViewController* controller = nil;

Then in your case statements you just assign to the existing controller variable instead of declaring it in that scope. 然后在您的case语句中,您只需分配给现有的controller变量,而不是在该范围内声明它。

- (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 ...
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM