Switch statement not working

160 views Asked by At

Switch statement is not working. Using switch statement to update the views ater timer is invalidated. In switch statement it is supposed to switch views from first to second view but it is not doing so.

@property (nonatomic, assign) NSUInteger viewControl; 

@synthesize viewControl;


 -(void)playpauseAction:(id)sender 
{

  if  

  ([audioPlayer isPlaying]){

 [sender setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateSelected];

 [audioPlayer pause];

 [timer invalidate];

  } else {

 [sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];

 [audioPlayer play];

self.timer = [NSTimer scheduledTimerWithTimeInterval:11 target:self selector:@selector(displayviewsAction:) userInfo:nil repeats:NO];     
  }  

}

- (void)displayviewsAction:(id)sender
{
switch(viewControl)
{

 case 0:
 [self performSelector:@selector(FirstViewController) withObject:nil];

 break;

 case 1:

 [self performSelector:@selector(secondViewController) withObject:nil];

 break;  

 case 2:  

 [self performSelector:@selector(thirdViewController) withObject:nil];

 break;
}          
}

-(void)FirstViewController {
FirstViewController *viewController = [[FirstViewController alloc] init];

viewController.view.frame = CGRectMake(0, 0, 320, 480);

[self.view addSubview:viewController.view];

[self.view addSubview:toolbar];

[viewController release];

self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:@selector(secondViewController) userInfo:nil repeats:NO];

}

-(void)secondViewController {
SecondViewController *secondController = [[SecondViewController alloc] init];

secondController.view.frame = CGRectMake(0, 0, 320, 480);

[self.view addSubview:secondController.view]; 

[self.view addSubview:toolbar];

[secondController release];

self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:@selector(ThirdviewController) userInfo:nil repeats:NO];

}

Any ideas what is missing in the code.

3

There are 3 answers

0
Perry On

The switch is over a variable named "viewControl" that doesn't seem to be defined anywhere in the code. You are going to have to give us more information to make a proper answer possible.

2
CodaFi On

Switch statements like your are meant for integer values (hence case:1,2,3,etc.). You are not even passing anything to the method when the timer invalidates, so you could use a switch with class names, random data, etc. and it would still never work. Use BOOLean values instead.

0
Caleb On

Add a default: case to your switch statement that logs the value that you're switching on, like this:

switch (viewControl) {
    case 0: {
        //...
        break;
    }
    case 1: {
        //...
        break;
    }
    default: {
        NSLog(@"Uh oh! The value I'm switching on isn't what I expect! viewControl == %d", viewControl);
        break;
    }
}

That won't fix your problem, but it'll help you figure out what's going on.