从另一个类访问创建的类对象

时间:2012-07-29 18:30:30

标签: iphone objective-c xcode class object

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类中方法的音乐?

1 个答案:

答案 0 :(得分: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];
}

祝你好运!