A couple of weeks ago i have started my research in Iphone app development and after alot of hello world applications in different settings i am now ready for my very first application based on the MVC design pattern used in Cocoa.
This opens up alot of questions for me though and after reading the different class references about UIViews and controllers i am stuck in trying to figure out which one i should be using.In my application i am trying to create a grid of small rectangle's with each rectangle having a different text value on them, to be more specific, i am trying to create a simple calender that will display all the days of a month in a grid.
Every rectangle is a instance of a class i named Tile, in this class i want to implement the drawRect method to draw the rectangle for me and set the text value to the day it should represent.In order to implement this i have done some research on how this should be done.
From what i have learned so far is that UIViewcontrollers do not really display anything, they are basically sitting there wait开发者_如何学运维ing to respond to any events from their children. In my application i would translate this to the Controller that will respond to each touchevent on a tile. A UIView however is also a container but one for objects that will need drawing methodes like drawRect. This would translate to the grid that will hold all of the tiles if i'm correct. Except, i have no clue what subclass i should use for each tile, i have the feeling i am really missing some basic knowledge here but i just can't figure it out. Would really appreciate it if anyone could point me in the right direction with this.If there were any two apple document you should read, it is the one about UIViewController
s which can be found here and the one about UIView
s which can be found here. The UIViewController
, as you mentioned, is more about integrating with the iOS system than being a visible component. It has a reference to a UIView
, and that UIView
is the root node in the visible tree of elements which starts at that View Controller.
In iOS programming you don't really need to worry about drawing rectangles, because for the most part you will be extending elements which know how to draw themselves and then just telling them where to go. The basic visible element in this case, is the UIView
. There are many different kinds of UIView
s (see the graphic in the UIView programming guide link), so for your case you could use a simple UIView
with a background image set to your calendar box graphic, and add a subview of type UILabel
. UILabel
is a subclass of UIView
, so you know it will be something visible as well.
Once you grasp these concepts (which can take a long time) Interface Builder
will start to make more sense and you can start doing some of these things with it - and understand how its working. In essence it will create the hierarchy of a UIViewController
referencing a hierarchy of UIView
s automatically, then you.
Tile
should be subclass of UIView
since you want to drawRect
your "days". Then you can add as many Tiles as you want to your UIViewController.view
and manipulate them from UIViewController
code (.m file).
But you can add UILabels
to your Tile
view and manipulate them by setting their text property. In this case you won't need to override drawRect:
at all, UILabel
will do the rest for you, but you will have to programmatically add this labels to your Tile
(e.g. in Tile's init
method) or in Interface Builder. In the last case you will have to load them from XIB using [[NSBundle mainBundle] loadNib:owner:options:]
method.
精彩评论