01 Dec, 2009
Hooking things together with controllers (day 2 of iPhone learning)
Posted by: Chris In: Random thoughts
After becoming a bit more comfortable with objective C and building a few of my own classes I think it is now time to move on to actually making a really basic user interface.
I wont go in to the details of how to actually lay an interface out. It’s really self explanatory, at least for basic stuff and there are tons of tutorials on the web. To get started you double click the MainWindow.xib in your project, which will launch Interface Builder.
A lot of the stuff today I have learnt from reading chapter 3 of “Beginning iPhone 3 Development” by Apress
A quick tip!
After reading some of my book I have learnt you can get nice MSDN like documentation help on API things within xcode by holding down the option key and double clicking on a keyword
MVC
The Model-View-Controller design pattern is very well documented and I wont explain it here. All there is to explain is that it is an approach very strongly used in iPhone development. It is a pattern I am very used to using after doing quite a lot of work with ASP.NET MVC.
When you create a View based application in XCode, a controller and a view is made for you automatically. You can find the controller files in Classes/MyProjectNameViewController.h and .m . The view is defined by using interface builder.
You write code in the controller to respond to UI events with your model, which will be classes that you will write yourself.
Outlets
Outlets are variables that you declare in your controller class to connect to controls in the interface builder, so that you can manipulate them in code
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
Once you have defined your outlet in your header file and synthesized it in your main file you can make your connect to your view in the interface builder.
Open the interface builder hold control and click on the “File’s Owner” icon shown in the myProjectViewController.xib window. Drag on to the control you want to make the connection with. When you release the name of your outlet should appear in a dropdown menu, select it.
Find the appropiate event, for pressing a button for instance it is “touch up inside” (tee hee). Click and hold on the radio button to the right of it and drag it to the FileOwner box, release and select the action you want associated with it.
Actions
These are methods declared in your controller class which can be triggered by controls.
- (IBAction)doSomething:(id)sender;
To assign actions to controls, select the control and press Apple+2 to open the connection inspector, which displays a number of events you can hook actions to.
All you have to do then is write an implementation of the method to make interactions happen, using outlets to modify the UI as appropiate
Please release me, let me go
You should remember that there is no garbage collection with Objective-C for the iPhone. So it is important that when you make objects that you release them when you are done with them. With outlets that you have made you should add a [myOutlet release] on the - (void)dealloc method of your controller.
Next…
In the next post will be details on performing more advanced user interface techniques



