2

First, I am not sure if I'm asking this within the correct Stack Exchange community, so if this question belongs somewhere else, please let me know. It didn't seem appropriate for Stack Overflow.

I am developing an audiobook app for iOS that uses a navigation controller to navigate between two TableViewControllers. The first view controller holds a list of books to choose from, and the second holds the list of chapters of those books. When a user clicks the play button of a chapter row, it starts to play the audio for that chapter.

At the bottom of each view controller I've created a custom view that holds the components of a standard audio player (play/pause button, track slider, duration, labels for the current track playing):

First View Controller - Book Selection Second View Controller - Chapter Selection

Each view controller has its own instance of the audio player view; there isn't a shared, global audio player view (I don't think you can even do this in iOS?).

I would like the state of the audio player at the bottom of both controllers to maintain a shared state as a user navigates around the app. For example, a user plays the Chapter 1 row. I need the audio player view at the bottom of each controller to display that the Chapter 1 entry is playing.

How do I achieve this? I'm fairly new to iOS development, but I believe I need to define a delegate protocol for the audio player view and have each view controller implement it? Or is there a better way of implementing my audio player; maybe creating a custom UIToolbar?

  • Starting a question with "I'm not sure if this is the right place to ask" is always a bad sign. – Robert Harvey Jan 20 '17 at 15:33
2

This is as good a place to ask as any as far as I can tell. First let's clear up the implied question.

It is quite possible to have a shared audio player view. I would do it by making a custom container view controller that holds a UINavigationController and a view with the player controls. It might be possible to sub-class UINavigationController and just put the player view directly on it's view as well.

If you go by either of the above ideas, then your problem is solved for this special case, but the question you are asking is still a good one to ask. I expect that at some point in your career you will have to share state among multiple view controllers and it's important you know how.

Depending on your experience the mere fact that I phrased your question as one of "sharing state" might have prompted you to generate your own answer. If not, I encourage you to think about it before continuing to read below.


The simplest way to share state among view controllers is by literally sharing an object that contains the state between those view controllers. A better method is to setup an Observer Pattern on your model Subject and have both view controllers attach themselves as Observers to the model. When the model changes state, the view controllers will be notified and can update their view states accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.