Custom UITabBarController and UINavigationController

后端 未结 4 523
心在旅途
心在旅途 2020-12-22 05:04

I\'m developing an app for iOS5 and up and I don\'t use storyboards or IB. I\'m creating a custom UITabBarController and in my AppDelegate I\'m put

4条回答
  •  天命终不由人
    2020-12-22 05:18

    Suppose you have a tabbarController. Now you can add any viewController or any NavController in your tabController. NavController can contain viewController. But you might have confusion where you will use navController or viewController. You will use viewController where you don't need navigation, I mean where you don't need.

    Here is an code example where the first view contains only view and the second view contains navigation controller. You can't push new view in first view, but you can easily push new view in second view.

    -(void)addTabBarControllers
    {
        UIViewController *viewController1, *viewController2;
    
        viewController1 = [[[HomeView alloc] initWithNibName:@"HomeView" bundle:nil] autorelease];
        viewController2 = [[[FloorPlanHome alloc] initWithNibName:@"FloorPlanHome" bundle:nil] autorelease];
    
    
        UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
    
        self.tabBarController = [[[UITabBarController alloc] init] autorelease];
        self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, nav2, nil];
    
        [[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:@"First View"];
        [[self.tabBarController.tabBar.items objectAtIndex:1] setTitle:@"Second View"];
    
    
        [[self.tabBarController.tabBar.items objectAtIndex:0] setImage:[UIImage imageNamed:@"first.png"]];
        [[self.tabBarController.tabBar.items objectAtIndex:1] setImage:[UIImage imageNamed:@"second.png"]];
    
        self.window.rootViewController = self.tabBarController;
        [self.window makeKeyAndVisible];
    }
    

    Call this method from didFinishLaunchingWithOptions in AppDelegate. here HomeView and FloorPlanView is two different views, you need to add these views and class file first.

提交回复
热议问题