Pregunta

I am developing this music streaming application for iOS. Everything is working great but haven't yet implemented the audio player functionality. I'm dealing with some problems on how to have the same audio player as a child view controller in all of my view controllers in the entire app. I'm not quite sure, but I think I have to use a singleton?.

And how do I set up the audio player to be the same when I change view controller?

enter image description here

¿Fue útil?

Solución

You need to get your terms and concepts right. An audio player is not a view controller, so it will not be a child view controller of anything.

What you want to do is to create a sound manager object and make it a singleton. (Do a google search on the singleton design pattern in Cocoa).

A singleton typically has a class method that let's you fetch it:

+ (MySoundManager *) sharedSoundManager;
{
  static dispatch_once_t once;
  static id _theSharedSoundManager;
  dispatch_once(&once, ^{
    _theSharedSoundManager = [[self alloc] init];
  });

  return _theSharedSoundManager;
}

And within your VCs, you get a pointer to your shared sound manager with a call like this:

[MySoundManager sharedSoundManager];

Then add methods to your sound manager to play sounds at the request of it's clients.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top