开发者

How do you add a UITabBarController to an existing project

I have an iPhone project that starts out with a standard UIView based Window... wh开发者_运维百科en the user clicks a button its suppose to launch into a new view with a UITabBarController -- similar to the way the iTunes Connect app behaves after you login. There are no sample code examples in the Apple documentation doing what I want but I know its possible because Apple has done it in their own apps (another example is the MobileMe iDisk app for iPhone).

I already tried the standard -presentModalViewController:animated: method and that did not work because there isn't a view that I can attach within the UITabBarController.

Next I am going to attempt to work with two window XIBs within the App Delegate to see if I can get that approach to work instead.

I would appreciate any insight if you know how to answer this little problem of mine. =)


What I ended up doing is this:

In my App Delegate, I have the following in my interface:

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow * window;
    LauncherViewController * startup;
    UITabBarController * tabs;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LauncherViewController * startup;
@property (nonatomic, retain) IBOutlet UITabBarController * tabs;
@end

In my implementation file, I add the following to the app start up function:

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

    [self.window addSubview:self.startup.view];
    [self.window makeKeyAndVisible];

    NSNotificationCenter * notifier = [NSNotificationCenter defaultCenter];

    [notifier addObserver:self
                 selector:@selector(launch)
                     name:MyAppLoginInitializedNotification
                   object:nil];

    [notifier addObserver:self
                 selector:@selector(logout)
                     name:MyAppLogoutNotification
                   object:nil];

    return YES;
}

- (void) launch {
    [self.startup.view removeFromSuperview];
    [self.window addSubview:tabs.view];
    [self.window makeKeyWindow];
}

- (void) logout {
    [self.tabs.view removeFromSuperview];
    [self.window addSubview:startup.view];
    [self.window makeKeyWindow];
}

My main XIB contains both the standard UIViewController defined as LauncherViewController as well as a generic UITabBarController. As soon as my main launcher controller authentices the user credentials and sends the MyAppLoginInitializedNotification, the app delegate switches from the launcher to the tab view enabling me to continue on with my app logic.


UITabBarController really is just a subclass of UIViewController, so -presentModalViewController:animated: should work:

UITabBarController *someController = [[UITabBarController alloc] init];
someController.viewControllers = /* your View Controllers here */
[self presentModalViewController:someController animated:NO];


if i understand your issue correctly, you want to start the UITabBarController View after the first view you mentioned in your Question, i am attaching a link doing the same thing you need except you have an Extra view before the UITabBarController View appears, hope it will give you a guide.

http://www.mobisoftinfotech.com/blog/iphone/iphone-tabbar-uitabbarcontroller-tutorial/


I don't think you have to re-add the UITabBarController in the nib file. Just create it in code, add it as the poster above says, and you should be good to go. Here's some code that works for me.

    UITabBarController *nextController = [[UITabBarController alloc] init];

    FirstController *firstView = [[FirstController alloc] initWithNibName:@"FirstView" bundle:nil];
    SecondController *secondView = [[SecondController alloc] initWithNibName:@"SecondView" bundle:nil];
    ThirdController *thirdView = [[ThirdController alloc] initWithNibName:@"ThirdView" bundle:nil];

    [nextController setViewControllers:[NSArray arrayWithObjects:firstView, secondView, thirdView, nil] animated:NO];

Till this point it should be the same, but I'm pushing a tabbar controller into the uinavgiationcontroller instead, so this is where we might differ. I do it as follows:

    [self.navigationController pushViewController:nextController animated:YES];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜