Saturday, April 16, 2011

Create view without InterfaceBuilder for iPhone

Although InterfaceBuilder is an important tool for iPhone development, the tool is not as intuitive to use as Visual Studio UI designer.
To help understand what interfacebuilder does, we can create the interface without using InterfaceBuilder, and then comparing the corresponding steps done by InterfaceBuilder.

Steps to add a ViewController without using InterfaceBuild:
1. Create a Window based iPhone application
2. Add a new UIViewController subclass in the project. Do not select "With XIB for user interface" checkbox
3. In the UIViewContollerSubclass' viewDidLoad method, create some UI elements and then add them into UIViewController.view collection by [self.view addSubview:uiElement] method.
4. In WindowAppDelegate.m didFinishLaunchingWithOptions method, create a instance of this new UIViewControllerSubClass instance, add it into appliation's main window with [window addSubview:uiViewControllerInstance.view].

Comparing with InterfaceBuilder to do the same thing, the following steps are required
1. If a UI sub class is created in the app for a ui element, like UIViewControllerSubClass, then the identity class of the UI element added in InterfaceBuilder needs to set the corresponding sub class with Identity Inspector.
2. For UIViewController object, use Attribute Inspector to set the NIB name to load the xib file, this associates the xib file with the UI control added in InterfaceBuilder.
3. Any IBOutlet property , or IBAction defined in .h file need to be associated with the corresponding ui element added in InterfaceBuilder.
4. File owner is the class that will load the xib file at runtime, for MainWindow.xib, it must be UIApplication classor its subclass. For UIViewController.xib, it must be a subclass of UIViewController class.

Jonathan

No comments:

Post a Comment