开发者

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-cas开发者_如何学Goe and put them at the end of the method.

The problem is, if I do this, I get the error: Use of undeclared identifier controller. I think it is because it is possible that the controller could remain undeclared (if no case is successful).

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?


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


}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜