iphone initializing tab bar controller view programmatically
I want to initialize my tab bar controller programmatically, but I just get a blank screen with the code I have. I tried to imitate TheElements sample app, and stuff seems comparable going line-by-line, but obviously something's wrong. Any suggestions?
In main.m:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"DubbleWrapAppDelegate");
[pool release];
return retVal;
}
In DubbleWrapAppDelegate.h:
@interface DubbleWrapAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@e开发者_运维百科nd
In DubbleWrapAppDelegate.m:
@implementation DubbleWrapAppDelegate
@synthesize window;
@synthesize tabBarController;
- init {
if (self = [super init]){
// initialize to nil
window = nil;
tabBarController = nil;
}
return self;
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
SafeTableViewController *vc1 = [[SafeTableViewController alloc] initWithStyle:UITableViewStylePlain];
[vc1 setSafeItems:[SafeItem knownSafeItems]]; // Set the list of known SafeItems:
UINavigationController *nc1;
nc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
[vc1 release];
BoxXRayTableViewController *vc2 = [[BoxXRayTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *nc2;
nc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
[vc2 release];
AboutLibertyViewController *vc3 = [[AboutLibertyViewController alloc] init];
UINavigationController *nc3;
nc3 = [[UINavigationController alloc] initWithRootViewController:vc3];
[vc3 release];
NSArray* controllers = [NSArray arrayWithObjects:nc1, nc2, nc3, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = controllers;
[controllers release];
// Add the tab bar controller's current view as a subview of the window
window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[window setBackgroundColor:[UIColor redColor]];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
[nc1 release];
[nc2 release];
[nc3 release];
}
The plist is set so that there is no NIB file referenced.
You should hold on to the window object. You marked it as autorelease, so it is released at the next application loop.
Don't call autorelease on window:
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
精彩评论