Custom iOS UIViewController (Universal)

These are the steps to create a custom UIViewControllers and .XIB files that take the place of the default MainWindow_iPhone.xib and MainWindow_iPad.xib in a Universal application.

Below is the architecture diagram. A custom HelloUniverseViewController will inherit from UIViewController. HelloUniverseViewController will not have a .XIB file. HelloUniverseViewController_iPhone and HelloUniverseViewController_iPad inherit from HelloUniverseViewController and have .XIB files. Functionality that is common between the two devices should be implemented in HelloUniverseViewController. Device-specific functionality should be implemented in the appropriate iPhone or iPad view controller.

Class Diagram

1. Create a new Window-based Application.
– Device Family = Universal

2. In the HelloUniverse folder, add a new UIViewController subclass.
– UNCHECK “Targeted for iPad”
– UNCHECK “With XIB for user interface”
– Save As “HelloUniverseViewController”

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

#import <UIKit/UIKit.h>

@class HelloUniverseViewController;

@interface HelloAppDelegate : NSObject <UIApplicationDelegate]]]]>

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

@end

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

#import "HelloUniverseViewController.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];
}

iPhone

5. In the iPhone folder, add a new UIViewController subclass.
– Subclass of HelloUniverseViewController
– UNCHECK “Targeted for iPad”
– CHECK “With XIB for user interface”
– Save As “HelloUniverseViewController_iPhone”

6. In the iPhone folder, open MainWindow_iPhone.xib.

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

Object List

|
|
V

Bar

8. Select newly added View Controller object.

UIViewController button

9. Go to the Identity Inspector and change the Class to HelloUniverseViewController_iPhone.

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

11. Add a label to the HelloUniverseViewController_iPhone.xib: “Hello Universe iPhone”

iPAD

12. In the iPad folder, add a new UIViewController subclass.
– Subclass of HelloUniverseViewController
– CHECK “Targeted for iPad”
– CHECK “With XIB for user interface”
– Save As “HelloUniverseViewController_iPad”

13. In the iPad folder, open MainWindow_iPad.xib.

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

Object List

|
|
V

Bar

15. Select newly added View Controller object.

UIViewController button

16. Go to the Identity Inspector and change the Class to HelloUniverseViewController_iPad.

17. Go to the Connections Inspector and add a New Referencing Outlet, viewController = Hello Universe App Delegate i Pad

18. Add a label to the HelloUniverseViewController_iPhone.xib: “Hello Universe iPhone”

19. Save, Build and Run

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

Tags: , ,

Leave a Reply