2012-02-27 16 views
0

Switch文が機能しません。 switch文を使用してビューを更新すると、aterタイマーが無効になります。 switchステートメントでは、最初のビューから2番目のビューに切り替えることになっていますが、そうしていません。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]; 

} 

コードには何も表示されません。

+2

switch文以外の 'viewControl'への参照はありません。その価値は何で、それはどこから来たのですか? – UIAdam

+0

読みやすいようにコードをインデントしてください。 – Caleb

+0

"viewControl"の設定を表示するには、displayviewsAction内にlogステートメントを追加することを検討してください。私はそれがあなたが思うものではないと思う。 –

答えて

2

スイッチが "viewControl"という変数上にあり、コード内のどこにも定義されていないようです。適切な答えを可能にするために、より多くの情報を提供しなければなりません。

1

あなたのようなswitch文は、整数値を意味します(したがって、1,2,3など)。タイマーが無効になったときにメソッドに何も渡していないので、クラス名やランダムなデータなどでスイッチを使うことができます。それでも決して動作しません。代わりにBOOLean値を使用してください。

+0

私は正確に私が間違っていると私はそれを修正する方法を教えてください前に私はスイッチのステートメントを使用したことがない。 – user1120133

+1

switch文は単なる一種の多分岐if文です。各「ケース」は、分岐している変数の値に対応します。 – Perry

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

あなたの問題を解決しないだろうが、それはあなたが何を見つけ出すお手伝いします行っている。