Adding a navigation based app as a TabBar tab

Discussion in 'iOS Programming' started by UberMonjie, Feb 11, 2009.

  1. UberMonjie macrumors newbie

    Joined:
    Feb 11, 2009
    #1
    Heya, first post here... thankful that there's a good community around iPhone development.

    I have a Navigation based app that runs just fine, however, I realized late in the development cycle that I am going to need a couple tabs. Is it possible to use the RootViewController from my navigation based app on a tab in a new TabBar?

    I read that it's easier to start a new app, TabBar based, and implement the old code into the new app, but I haven't had any luck with that.

    Thoughts?

    Thanks again!
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    Yup, each tab contains a view controller so your only issue is dealing with the frame being a little shorter (50 pixels shorter in fact).

    Probably easiest, depending on how you've built your app. If you're mainly using IB then I'd go this route, if you're doing your view layout in code then you'll probably find it easier to modify your app delegate instead.
     
  3. UberMonjie thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #3
    Thanks...

    Right on..

    I got a tabbar added to my original app, without IB, trying to keep everything in code. I'm just confused as how to wire my RootViewController to a tabBar tab?

    I am reading some basic tabBar tutorials now. Any suggestions or tips that may make this process easier?

    Thanks again, stoked to have found this community, and look forward to contributing.
     
  4. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #4
    In your AppDelegate, something like:
    Code:
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UITabBarController *tbc = [[UITabBarController alloc] init];
    
    // Init your view controllers here.	
    RootViewController *rvc = [[RootViewController alloc] init];
    AnotherViewController *avc = [[AnotherViewController alloc] init];
    
    // Give them titles and pretty icons.
    rvc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Root" image:[UIImage imageNamed:@"icon_root.png"] tag:0];
    avc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Another" image:[UIImage imageNamed:@"icon_another.png"] tag:1];
    
    // Add to the tab bar (animated: is a slidy animation when you launch).
    [tbc setViewControllers:[NSArray arrayWithObjects:rvc, avc, nil] animated:YES];
    
    [window addSubview:tbc.view];
    [window makeKeyAndVisible];
     
  5. UberMonjie thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #5
    Wow...

    Wow, I have been reading multiple tutorials and message boards posts all over the web on this issue, and have not found anything that worked. I found plenty of confusing undocumented code, that didn't work.

    That small code snippet was exactly what I was looking for, worked as expected, perfect. Many thanks!

    I hope to contribute to this community and help rookies like myself very soon. Thanks again!
     
  6. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
  7. UberMonjie thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #7
    So, now...

    I have been playing with this a lot, and reading as much as possible on the matter, but I can't figure out a couple things. My setup is as follows:

    - app delegate
    - uiview controller1
    - uiview controller2
    - navbar controller

    In the app delegate I set up the tabbar, and I can load in both uiview controllers in separate tabs, and I can load my navbar controller in another tab also. Questions:

    1. The navbar appears, but I cannot edit the title, add buttons, etc. Do I do that stuff in the app delegate, or the navbar controller? somewhere else?
    2. I also am not sure how to load uiview controller1 into the tabbar controller, so tab1 loads navbar controller which loads uiviewcontroller1.

    Make any sense? Thanks!
     
  8. UberMonjie thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #8
    Well..

    I figured out how to load a uiview into the navbar controller view, but I still can't add a title to the navbar, or add buttons?

    Code:
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    
    	//UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    	UITabBarController *tabBarController = [[UITabBarController alloc] init];
    	
    	// Init your view controllers here.	
    	GabViewController *gvc = [[GabViewController alloc] init];
    	VoteViewController *vvc = [[VoteViewController alloc] init];
    	
    	UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gvc];
    	nav.navigationBar.barStyle = UIBarStyleBlackOpaque;
    	nav.navigationItem.title = @"Sayonara Chat";
    	
    	// Give them titles and pretty icons.
    	nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Gab" image:[UIImage imageNamed:@"icon_root.png"] tag:0];
    	vvc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Vote" image:[UIImage imageNamed:@"icon_another.png"] tag:1];
    	
    	// Add to the tab bar (animated: is a slidy animation when you launch).
    	[tabBarController setViewControllers:[NSArray arrayWithObjects:nav,vvc, nil] animated:YES];
    	
        //[window addSubview:navigationController.view];
    	[window addSubview:tabBarController.view];
    	[window makeKeyAndVisible];
    }
    
    Am I trying to set the "nav" title the wrong way or something?
     

Share This Page