Choose Window-based Application
Add a main UIViewController subclass without making a .xib file.
I called it mainViewController.
Make one mainViewController subclass for each of the iphone or ipad App respectively with .xib file option this time. I called them ViewController_iphone and ViewController_ipad respectively.
Here are the files that we have for this project.
Nothing need to be done with aProjectNameAppDelegate at this point.
In aProjectNameAppDelegare_iPhone.h:
#import <UIKit/UIKit.h>
#import "aProjectNameAppDelegate.h"
@class ViewController_iphone; //added
@interface aProjectNameAppDelegate_iPhone : aProjectNameAppDelegate {
}
@property (nonatomic, retain) IBOutlet ViewController_iphone *iponeViewController; //added
@end
In aProjectNameAppDelegare_iPhone.m:
#import "aProjectNameAppDelegate_iPhone.h"
#import "ViewController_iphone.h" //added
@implementation aProjectNameAppDelegate_iPhone
@synthesize iponeViewController=_iponeViewController; //added
// This method was added and overrided
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.iponeViewController;
[self.window addSubview:self.window.rootViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_iponeViewController release]; //added
[super dealloc];
}
@end
In ViewController_iphone.h:
#import <UIKit/UIKit.h>
#import "mainViewController.h" //added
@interface ViewController_iphone : mainViewController {
}
@end
In mainViewController.m override the viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor]; //added
}
For ipad, follow the same steps.
In MainWindow_iphone.xib, add a View Controller icon, and change its class name to View Controller_iphone
Press down Control key and drag from the App Delegate icon to the View Controller icon and select iphoneViewController for the Outlet.
Here is what you will get.
and
For this application, it will also work with only the mainViewController. However, I find this method is more versatile.









Very good post! I'm studying iOS.
ReplyDeleteDid you can you explain more "Press down Control key and drag from the App Delegate icon to the View Controller icon and select iphoneViewController for the Outlet."
Hi. I just tried following this on XCode 4.1 running under Lion and it fell apart after adding mainViewController. I can add it but then get a load of errors. Cannot find interface declaration for mainViewController, superclass of mainViewController. No idea what is going on really.
ReplyDeleteHas anyone got this to work under Lion and XCode 4.1?