2

I'm rather new to iPhone development, and am wondering how to load multiple views from a single controller.

I come from a c# background and still trying to get my head around how things work in ObjC and the iPhone worlds.

As a basic example of what I am trying to achieve, lets say I have a ClockController, the clock controller has the base methods for calculating the time.

Now what I want to do is have this controller capable of rendering any number of views which support the ClockContollers underlying interface.

Lets say I have one view that renders the clock as a digital output and another that renders it as an analog display (basic example I know).

How would one achieve this?

Sorry if this is a very basic question, but I am quite new at this.

Thanks in advance

2 Answers 2

2

Here is my understanding of how to apply MVC to your example:
The clocks that you draw on the screen are inside views which are each backed by an instance of your clock view controller class. These controllers each interact with a clock model and update their views as appropriate. It is in this class that a property would be set that determines if the clock is digital or analog.
It sounds like your "ClockController" is really a model/data class. You can have just one instance of this model class or one for each of your clock view controllers (if they have different times).

1

In my apps I have many instances of a single view controller which then utilizes different or multiple views to display the UI. You can take a view and all its subviews and add it to the view controller using addSubView, and remove that view and all its subviews using removeFromSuperview. This works well if the view is only displaying info and is subclassed to handle its own differences from the other views that the view controller has to handle. In you case, an analog clock view has to draw itself one way and the digital clock view draws itself in another. The controller merely has to tell each view what the time is, and the view handles the translation to visual data. If your view accepts input in the form of a user dragging the analog hands to set the alarm, then the view subclass needs to read that input and then translate to an alarm time and pass it back to the view controller. The controller doesn't need to know which type of clock the user is interacting with, but only sees an alarm time it needs to add to its alarm queue.

However, when I have views that utilize very different UIs or for different purposes, I generally prefer having one view controller per set of views. Basically, I try to use my views as a display and my viewControllers as the manager of data related to that view (and any other views that can be backed by that same data).

For example, my game has one view controller for playing, another for the high score table, and yet another for the settings.

1
  • Cool! Thanks for the explanation guys. That's just confirmed my ideas. Thanks for the addSubView reference, I'll be checking the doco for usage details. I think my main challenge will be getting used to how things hang together... plus learn to be vigilant cleaning up those resources ;-)
    – Al.
    Oct 22, 2009 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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