I've got a situation where I can think of several ways of doing what I need to do, but I don't really have the experience to judge which is best or why as of yet, so I'd really like some suggestions as to what you guys would prefer. This is a pretty simple app, but I want to do this one right so that when I write larger apps later I'll know how to do it without going through all this again.
I've got an iPhone app using the Utility Application template (two views (a front and a back), controllers for each, a root controller that inits the two views, and an app delegate).
I have a simple modal class that I wrote which stores some counters and has some methods to increment, decrement those counters, etc.
I need both sides of the app (both views) to be able to deal with the same instance of the modal, so one side can display a value which can be changed by the other side, etc.
My question is about the best way to make sure that both sides have access to that instance of the modal, and where to store said instance of said modal.
I could make my modal a singleton, thereby letting each controller access the same instance all the time. Views could each observe the necessary values and tell the app to perform various methods when actions are invoked.
I could let the app delegate init the Modal and have the views each get a pointer to that modal when they're inited using modal = [[UIApplication sharedInstance] delegate].modal; again, views could each observe the necessary values and tell the app to perform various methods when actions are invoked. In some ways I like this one best except that it assumes that the app delegate will always return the same address/instance when it's asked for the modal, which although it should is not guaranteed.
I could let the app delegate be an intermediary between the view controllers and the modal. Any time the views needed to update something they'd send a message to the app controller which would send a message to the modal. The app controller would also observe the necessary values and tell the view controllers to update their views when those values changed. On the other hand, would this preserve MVC better since only one class (the app delegate) would need to be updated if the Modal was changed?
I'm definitely over thinking this, but I'm still trying to understand how to use MVC and best respect encapsulation in an application I'm writing myself from scratch.
So anyway, how would you guys set up such an app? Am I missing something completely obvious?
I've looked at all of the apple apps, but most of them don't really seem to have a real modal class, and the ones that do don't need that modal to be accessed from multiple places in the app.
I've got an iPhone app using the Utility Application template (two views (a front and a back), controllers for each, a root controller that inits the two views, and an app delegate).
I have a simple modal class that I wrote which stores some counters and has some methods to increment, decrement those counters, etc.
I need both sides of the app (both views) to be able to deal with the same instance of the modal, so one side can display a value which can be changed by the other side, etc.
My question is about the best way to make sure that both sides have access to that instance of the modal, and where to store said instance of said modal.
I could make my modal a singleton, thereby letting each controller access the same instance all the time. Views could each observe the necessary values and tell the app to perform various methods when actions are invoked.
I could let the app delegate init the Modal and have the views each get a pointer to that modal when they're inited using modal = [[UIApplication sharedInstance] delegate].modal; again, views could each observe the necessary values and tell the app to perform various methods when actions are invoked. In some ways I like this one best except that it assumes that the app delegate will always return the same address/instance when it's asked for the modal, which although it should is not guaranteed.
I could let the app delegate be an intermediary between the view controllers and the modal. Any time the views needed to update something they'd send a message to the app controller which would send a message to the modal. The app controller would also observe the necessary values and tell the view controllers to update their views when those values changed. On the other hand, would this preserve MVC better since only one class (the app delegate) would need to be updated if the Modal was changed?
I'm definitely over thinking this, but I'm still trying to understand how to use MVC and best respect encapsulation in an application I'm writing myself from scratch.
So anyway, how would you guys set up such an app? Am I missing something completely obvious?
I've looked at all of the apple apps, but most of them don't really seem to have a real modal class, and the ones that do don't need that modal to be accessed from multiple places in the app.