View or ViewController... both are views?
In Interface Builder... when I drag a VIEW into the document window... and then double-click on it... it opens and displays the VIEW window. (As expected)
... but when I drag a VIEW CONTROLLER开发者_开发技巧 into the document window... and then double-click on it... it also opens and displays the VIEW window, too. (It says "view" right on it.)
Is that right? (Or am I totally misunderstanding things?)
I thought a VIEW was the actual object to draw/drag things into. No?
I thought a VIEW CONTROLLER was just the CODE for your view. No?
I dont have experience with Interface Builder, but a a controller object provides the custom logic needed to bridge the application’s data to the views. In iPhone applications, a view controller is a specific type of controller object that you use to present and manage the views of your application.
Each ViewController has a View property associated to it, which is the one you are seeing in interface builder.
The view stored in this property represents the root view for the view controller’s view hierarchy. Whenever you present the view controller on screen (either modally or as part of view controller–based interface), this view is retrieved and displayed in the application window. The default value of this property is nil
Each view controller object is the sole owner of its view. You must not associate the same view object with multiple view controller objects.
Apple wants you to follow the Model-View-Controller system when you develop apps, and it's pretty straightforward and logical.
- The Model is the core of your app. It processes databases, network connections and whatever you need. It's basically custom classes you create in Xcode.
- The View is the interface between your app and your users. You can create them in Interface Builder and put them in NIB files (preferably one view every file).
- The Controller is the glue between your view and your model. It defines the behavior and state of views (button enabled, label content, etc.) based on what it gets from the model (like a database). It also performs actions on the model based on the events it receives from the views it managers (controls), like changing records in a database or changing variables in objects.
The idea behind all this is that the model can be used in every platform with minimal modification.
Every controller should be linked to one view, and one view only, like a table (UITableViewController
) or a screen's view (UIViewController
). You subclass the controller you want and you add it to the NIB of the view it's associated with. That's how you do it:
- In the NIB, select the File's Owner.
- In the Identity inspector, set the class to the view controller you created in your project.
- In your custom controller class, create
IBOutlet
s for every view (such asdeleteButton
) in the associated view you need to have access to. Create and synthesize the properties for every outlet. - Create
IBAction
s for every event you want to register (such asaddButtonClicked
). - In the NIB, drag a line with your secondary mouse button from the File's Owner to the wanted outlet view and then select which connection you want to make. Repeat until all your
IBOutlet
s are properly connected. - Drag a line with your secondary mouse button from the view you want to register events from to the File's Owner and then select which connection you want to make. Repeat until all your
IBAction
s are properly connected. - Write your code for the controller.
精彩评论