This may be a basic question but I'm a newby at IOS development..
I need a background music for my iPad application and I need to stop it at another UIViewController. I Start my background music just like that at my MainMenuViewController.m file
NSString* pathToBackGroundMusic = [[NSBundle mainBundle] pathForResource:@"MenuBackGround"开发者_JS百科 ofType:@"mp3"];
backGroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathToBackGroundMusic] error:nil];
backGroundMusic.numberOfLoops = -1;
[backGroundMusic play];
Now i need to stop it when I press a button at an another UIVIewController CustomGameMenuViewController.m... I'm sorry if it is already answered but i couldn't find it... Any help will be appreciated. Thanks.
There are a couple of ways to do this. The simplest way would be to use NSNotifications. Look up the documentation on NSNotification and NSNotificationCenter. With this method the original view controller would register to listen for a certain notification and your other controller would send that notification when it needed to stop music.
Another route would be to set up the first view controller as a delegate or property of the second one. This is a little more involved and convoluted. If this is the only communication back and forth between these controllers I would go the notification route.
精彩评论