2012-07-29 109 views
0

ViewController1.h獲取從另一個類訪問創建類對象

@interface ViewController : UIViewController 
@property AVAudioPlayer *audioPlayer; 
@end 

ViewController1.m

@synthesize audioPlayer; 
... 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
[audioPlayer play]; 

我怎樣才能阻止類ViewController2方法的音樂嗎?

回答

0

使用NSNotification:

將這個當用戶點擊ViewController2停止音樂:

[[NSNotificationCenter defaultCenter] postNotificationName:@"stopMusic" object:nil]; 

和ViewController1應該有這樣的:

-(void)viewDidLoad: 
{ 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopTheMusic:) name:@"stopMusic" object:nil]; 
} 

-(IBAction)stopTheMusic:(id)sender { 
     [audioPlayer stop]; 
} 

祝您好運!

+0

工作就像一個魅力!你是最棒的。 – user1561346 2012-07-29 18:48:52

+0

@ user1561346沒問題!很高興幫助! – sridvijay 2012-07-29 18:50:04