Tim
Tim

Reputation: 13258

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.

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?

Upvotes: 0

Views: 8061

Answers (2)

Max
Max

Reputation: 16719

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


}

Upvotes: 1

smorgan
smorgan

Reputation: 21579

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.

Upvotes: 1

Related Questions