Search code examples
iosuiviewcontrollersparrow-framework

Trouble understanding viewControllers in iOS


I'm new to obj-c/iOS and I'm having trouble understanding conceptually at least viewControllers. I've read a lot of the Apple Doc's, I've even used viewControllers to some extent in xCode, but I still don't quite get what they are, or what are the best ways to use them.

I've been an AS3 dev for many years so my mind works in the context of MovieClips/Sprites and the Display list to get graphics on the screen.

Ok so from my understanding...

  1. A viewController is a kind of class that handles graphics in some fashion and then allows you to do something with them?? What is it in it's most basic sense?
  2. You seem to add viewControllers to a Window class, which I guess is a bit like adding a display Object to the Display list?
  3. What is it that a viewController does for you in it's most basic sense?
  4. Are there certain things you definitely can't do with them or shouldn't do with them?
  5. Do viewControllers need to be connected in some way to the rest of the iOS framework to function (apart from being added to a window).
  6. How exactly do they use data? (I've read up on MVC, I understand that conceptually this is a slightly different question) as I understand it you don't hardcode data into a viewController, so how does a viewController access any static data?
  7. Let's say I just wanted to throw an image up on the screen, exactly what part would the viewController play in that process? is it just something which handles only one small aspect of that process or is it the whole show and handles everything?
  8. Does one viewController handle multiple images? is it like it's term, a "controller" for all the images presented on screen, or does it handle one image at a time?
  9. What is a viewControllers connection to the image(s) it handles? it contains references to them?

I'm using the Sparrow framework which is helping but I would still like to be able to get my head around what viewControllers are so I know how to use them properly.

Ha, I apologise for the above I know it must look like I'm completely confused :) thanks for any advice.


Solution

  • Hope this helps you:

    1. A viewController is a kind of class that handles graphics in some fashion and then allows you to do something with them??

      It's the glue between a View (Xib File) and the Data (Could be CoreData or whatever you're using in the backend). All the UI Elements you are using in the View you normally define as properties in the controller to get access to them.

    2. What is it in it's most basic sense? You seem to add viewControllers to a Window class, which I guess is a bit like adding a display Object to the Display list?

      I don't really know AS3 so I cannot compare Display lists with ViewControllers. But basically ViewControllers are there to handle different types of transitions between the views and accessing (setting/reading) the data which is displayed in the view.

    3. What is it that a viewController does for you in it's most basic sense?

      Like I've written above. Most basic sense they interpret what the user does on the view and depending on the action of the user changes the model.

    4. Are there certain things you definitely can't do with them or shouldn't do with them?

      It is always hard to keep the border between model and controller. They are pretty close to each other. So what I normally try is to delocate all logic stuff (like calculations, database access and so on) this does more belong into the model part. But of couse you're using these external classes in the controller.

    5. Do viewControllers need to be connected in some way to the rest of the iOS framework to function (apart from being added to a window).

      Well like you already have written the ViewController needs to be connected to a view. Otherwise it would not make much sense. There are different subtypes of UIViewController such as UINavigationController where you probably need to overwrite some other methods to provide the whole functionality wanted by these special subtypes.

    6. How exactly do they use data? (I've read up on MVC, I understand that conceptually this is a slightly different question) as I understand it you don't hardcode data into a viewController, so how does a viewController access any static data?

      There could be different approaches to store the data. Simplest way would be to have the data directly stored in the UIViewController. This could be a custom class which is the container of the data. All changes are directly written into this class and displayed by the UIViewController. But in most of the cases it makes sense to use CoreData (Which is responsible for reading/writing the data into a sqlite database). You could look at CoreData as your model and the UIViewController gets the data from there and passes the data which the UIViewController has received from the View back to it.

    7. Let's say I just wanted to throw an image up on the screen, exactly what part would the viewController play in that process? is it just something which handles only one small aspect of that process or is it the whole show and handles everything?

      The UIViewController would store an internal Property (UIImageView *) which is in the Interface Builder connected with the UIImageView you have created in the Xib file. So over these property you can change through your Controller the image.

    8. Does one viewController handle multiple images? is it like it's term, a "controller" for all the images presented on screen, or does it handle one image at a time?

      Yes, this isn't a big problem. You can have as many images you want. You just need to have the properties defined in the UIViewController and linked to the View.

    9. What is a viewControllers connection to the image(s) it handles? it contains references to them?

      Yeah, its like a reference to the UIElement. You can then change whatever property of the UIImageView you want directly from the UIViewController

    Some useful links: