0

I am making a sort of music application and the main view has the playback buttons at the top with other buttons too. This section only takes up the top section of the view as the other part has a container view in it.

I initially had buttons to switch from view to view but I need the audio to be played whilst on different views and to be able to play, pause etc while in the other views

How do I have the button in the main controller act on objects from the child view container?

Klaus
  • 27
  • 5

1 Answers1

0

There are a few ways of sending messages from one view controller to another.

  1. Singleton.
  2. Delegates.
  3. KVO.
  4. Notifications.

Since you're building an audio application, using a Singleton might be the best way forward.

Your singleton is instantiated once, keeps your playback functionality separate from your view controllers and can therefore be accessed from anywhere within your application.

Perhaps you have a play function in your singleton, which starts your audio, and a pause function that pauses it. You should be able to call these functions from any view controller.

It would be as simple as:

AudioSingleton.shared().play

Beau Nouvelle
  • 6,962
  • 3
  • 39
  • 54
  • Would this still be fine to play the audio if the button is playing audio from a different view? – Klaus Dec 06 '16 at 22:29
  • It's not the button thats playing the audio. The button is there for the user to interact with, which when tapped calls the `play` function in your singleton. If the button disappears, the audio will continue to play until the `pause` function is called perhaps from another user interaction. – Beau Nouvelle Dec 06 '16 at 22:30
  • The functions for the audio is also in the other view controller class file too. I tried using the " #import "ViewController2.h" " code into the main view controller's m file however no luck – Klaus Dec 06 '16 at 22:42
  • Yeah so you will want to move all that audio stuff out into its own class, preferably a singleton. Separate from your view controller. Because then, you'll be able to call the `play`/`pause` functions in ANY view controller. – Beau Nouvelle Dec 06 '16 at 22:46
  • Maybe this answer will help? http://stackoverflow.com/a/9880450/2114482 – Beau Nouvelle Dec 06 '16 at 22:47