How to separate UI and logic in Objective-C based mobile games?
To develop games, I use Cocos2d library.
I need a good reference to separate my UI code from game logic code.
The usual way to loosely couple complex objects in Objective-C is to use delegates with formal protocols.
In an iOS board game I'm working on, I have
And in the classes themselves, e.g.
Then, a controller implements both and mediates between them:
This controller is the delegate for both the game controller and the view. It handles
You can see many more examples of this style in Apple's own APIs. Almost all objects with non-trivial state or feedback use a formal protocol rather than inheritance or a big set of selector/target pairs.
Hmm...I'm not quite sure about this but I treat UI as MVC. I separate them into those domains. The game will only talk to the controllers so that you will not need to know anything about the view. That way the game logic, UI logic, and the view itself are all separated.
I actually did a CCView and CCViewController myself (though it needs more refinement). This helps me to change view whenever I want (esp. we have iPhone5 now).
The game itself has a different architecture. I'm using Component Design for the game.
I'm still not sure about this because I just made it up myself since it seems to work for me. If there are better approach, I'm open to it.