Switch语句不起作用

时间:2012-02-27 22:09:07

标签: iphone

Switch语句不起作用。使用switch语句更新视图ater计时器无效。在switch语句中,它应该将视图从第一个视图切换到第二个视图,但它没有这样做。

@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];

}

代码中缺少的任何想法。

3 个答案:

答案 0 :(得分:2)

开关位于名为“viewControl”的变量上,似乎没有在代码中的任何位置定义。您将不得不向我们提供更多信息以便做出正确的答案。

答案 1 :(得分:1)

像你这样的切换语句用于整数值(因此情况:1,2,3等)。当计时器无效时,你甚至没有向方法传递任何东西,所以你可以使用带有类名,随机数据等的开关,它仍然无法工作。改为使用BOOLean值。

答案 2 :(得分:1)

在您的switch语句中添加default:个案,记录您正在启用的值,如下所示:

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;
    }
}

这不会解决您的问题,但它可以帮助您弄清楚发生了什么。