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

Wednesday, April 6, 2011

How to hide and show soft keyboard on iPhone simulator

When testing with iPhone simulator, occasionaly, you will need to show or hide the soft keyboard. To do so, go to IOS simulator's Hardware menu, and select "Simulator Hardware Keyboard" submenu, it will toggle the soft keyboard's state.


Jonathan

Sunday, April 3, 2011

Protocol in Objective C

Protocol in Objective C is quite similar to interface in java or .net, so there is not much to say about it.

To define a protocol in a class header as
@protocol MyProtocol
-(void) myfunction;
@end

The header file of the class that implements a protocol looks like the following, notice the syntax is quite similar to category but uses angle bracket instead of parenthesis.

@interface MyUserClass : NSObject .

Jonathan

Category in Objective C

Category in Objective C is a new concept to C++ developer. The catetory must associate with any existing class, even if the class implementation is not accessible to you. Note that a cateory can not declare additional instance variables for the class. It can only include instance methods.
While category is similar to subclass, the difference is with category, the new methods directly added into the original class without creating a new derived class.

The syntax is

Header file (name convention is "classname+categoryname.h"):
#interface "ClassName.h"

@interface ClassName (CategoryName )
- (int) categoryFunction;
@end


Implementation file (name convention is "classname+categoryname.m"):
#import "classnmae+categoryname.m"

@implementation ClassName (CategoryName )
- (int) categoryFunction {
return 0;
}
@end

Note another Objective C feature: Extension is like cateory but without an name, the extension methods must be implemented in the main @implementation block of the corresponding class. The extension example is shown below:
Header file ( "classname.h")
@interface ClassName ()
- (int) extensionFunction;
@end

implementation file ("classname.m")
@implementation classname

... class methods implementation

- (int) extensionFunction {
return 1;
}

@end

Jonathan