Custom iOS UIViewController

These are the steps to create a custom UIViewController and .XIB that take the place of the default MainWindow.xib.

1. Create a new Window-based Application.

2. Add a new UIViewController subclass.
– CHECK “With XIB for user interface”
– Save As “HelloViewController”

3. Edit HelloAppDelegate.h. Add the bold lines below.

#import <UIKit/UIKit.h>

@class HelloViewController;

@interface HelloAppDelegate : NSObject <UIApplicationDelegate]]>

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet HelloViewController *viewController;

@end

4. Edit HelloAppDelegate.m. Add the bold lines below.

#import "HelloViewController.h"

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    self.window.rootViewController = self.viewController;

    [self.windowmakeKeyAndVisible];
    return YES;
}

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

5. Open MainWindow.xib.

6. From the Objects list, drag a View Controller object onto the bar.

Object List

|
|
V

Bar

7. Select newly added View Controller object.

UIViewController button

8. Go to the Identity Inspector and change the Class to HelloViewController.

9. Go to the Connections Inspector and add a New Referencing Outlet, viewController = Hello App Delegate

10. Add a label to the HelloViewController.xib: “Hello World!”

11. Save, Build and Run

Click here to download the sample XCode project. It targets iOS 4.3 for the iPad.

Tags: , ,

Leave a Reply