Himmi
Himmi

Reputation: 47

iOS communication ViewController -> Model

This is really a starter question but I somehow can't figure out a good way to solve this.

The problem: I want my ViewController to tell my "model" what sounds to play and when they should be changed and played.

What I have: A AudioPlayer class which I created and is just a subclass of NSObject. It sets up my audioSession and handless my apps sound progression. Now I basically just want my ViewControllers to be able to send an integer over to my AudioPlayer class and have that class perform a function that would swap in a new sound path for the player. The player is a object belonging to the AudiPlayer class and can't be called from a global/shared function.

What I've tried: Having global variables and setting them from the ViewControllers which doesn't solve my problem of calling a in class function. I actually got passed this problem with a repetitive timer that called an function from inside the class to check regularly if a global Boolean had been set to TRUE and then performed a sound swap but I find this very messy.. this can't be the optimal way?

I've been trying singletons (which my class is now).. not that it's helping me with this problem

I've also been looking at delegates but using them I always end up messing up my audioplayer delegate(I think!), audioplayer ends up playing nothing(but seems to run fine over each command). Debugging the player setup shows though: audioplayer error: unknown class.

Any suggestions or good answers to this problem are very well appreciated. Thank you

Upvotes: 1

Views: 75

Answers (1)

Pieter
Pieter

Reputation: 17705

The model shouldn't "play" (or do anything really), it's just the data.
So instead of telling your model what to play, your viewcontroller should ask the model what needs to play, and play itself.

As I understand your question, your AudioPlayer is more than a 'model' class, (as it plays audio), but to give decent comments on your classes we would need to see code obviously.

Upvotes: 1

Related Questions