2012-05-04 6 views
0

私はデバイスを振ると音が出るプログラムを作成しました。ボタンをタップすると、いつでもサウンドが再生されなくなります(これは「リセット」メソッドです)。別のボタンがあります。プッシュすると別のビューが表示されます(これは "infoView"メソッドです)。この問題は、「infoView」に行き、最初のビューに戻るときに発生します。デバイスが揺れても "リセット"ボタンが反応しなくなると、サウンドは再生されます。ここまで私がこれまで持っていたことは、どんな助けも認められるだろう。ビューを切り替えてスイッチバックした後にUIButtonが死んだ

PS。 FirstRespondersと何か関係があるのだろうか?私はまだFirstRespondersの周りに私の頭をラップしようとしています。

の.h

#import <UIKit/UIKit.h> 
#import "AVfoundation/AVfoundation.h" 

@interface OldTvViewController : UIViewController{ 
AVAudioPlayer *audioPlayer; 
} 
-(IBAction)reset; 
-(IBAction)infoView:(id)sender; 
@end 

.M

#import "OldTvViewController.h" 
#import "AudioToolBox/AudioToolBox.h" 
#import "infoViewController.h" 

@implementation OldTvViewController 

-(IBAction)infoView:(id)sender{ 
infoViewController *second = [[infoViewController alloc] initWithNibName:Nil bundle:Nil]; 
[self presentModalViewController:second animated:YES]; 
[second release]; 
} 

-(BOOL) canBecomeFirstResponder{ 
return YES; 
} 

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ 

if (event.subtype == UIEventSubtypeMotionShake) { 

    //Play throw sound 
    if(audioPlayer.playing){ 
     [audioPlayer stop]; 
    } else{ 
     NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/bomb.mp3",[[NSBundle mainBundle] resourcePath]]]; 
     NSError *error; 
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
     audioPlayer.numberOfLoops = 0; 
     [audioPlayer play];  
    } 

} 

} 


-(IBAction)reset{ 
[audioPlayer stop]; 
} 


-(void)viewDidAppear:(BOOL)animated{ 
[self becomeFirstResponder]; 
[super viewDidAppear:animated]; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
} 

@end 
+0

に追加します。 – vishiphone

答えて

1

あなたは、そのクラスでこのコードを試してみてください、あなたのすべてのボタンが利用できる情報ビューに移動します。そのクラスで利用可能な、あなたのすべてのボタンは、このコードを試してみてくださいどこその後、インフォビューに行くとき

-(void)viewWillDisapeers 
{ 

    [audioPlayer stop]; 

} 

またはこのメソッド

-(IBAction)infoView:(id)sender 
{ 
infoViewController *second = [[infoViewController alloc] initWithNibName:Nil bundle:Nil]; 
[self presentModalViewController:second animated:YES]; 
[audioPlayer stop]; 
[second release]; 
} 
関連する問題