I have 5 standalone table view controller nibs (with custom cell implementation) accessible through an another table view menu list (no storyboards)

The client desires to have all 5 nibs in tabs. So I need to get rid of the menu list and provide nibs in TABs .

how can I do this ?

有帮助吗?

解决方案

First add this property to your AppDelegate.h

@property (strong, nonatomic) UITabBarController *tabBarController;

make a method to set the views and set up your tabbar like:

-(void)setViews
{
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] ;
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil];
    UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:nil];

    UINavigationController *navigationController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
    [navigationController1.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];

    UINavigationController *navigationController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
    [navigationController2.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];

    UINavigationController *navigationController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
    [navigationController3.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];

    UINavigationController *navigationController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
    [navigationController4.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];

    UINavigationController *navigationController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
    [navigationController5.navigationBar setBackgroundImage:[UIImage imageNamed:@"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];


    [navigationController1.navigationBar setHidden:YES];
    [navigationController2.navigationBar setHidden:YES];
    [navigationController3.navigationBar setHidden:YES];
    [navigationController4.navigationBar setHidden:YES];
    [navigationController5.navigationBar setHidden:YES];


    self.tabBarController = [[UITabBarController alloc] init];
    [self.tabBarController.tabBar setBackgroundColor:[UIColor clearColor]];

    self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar"];
    [[[self tabBarController]tabBar]setSelectionIndicatorImage:[UIImage imageNamed:@"transparent.png"]];
    [self.tabBarController setDelegate:self];
    self.tabBarController.viewControllers = @[navigationController1, navigationController2,navigationController3,navigationController4,navigationController5];
    self.window.rootViewController = self.tabBarController;
}

avoid the set images and setHidden if you don't want to or not want to make the custom navigation bar. and call this method in your didFinishLaunchingWithOptions.

Now set up the delegate method for tabbar and you can set the custom images over there:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (tabBarController.selectedIndex == 0)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-1"];
    }
    else if (tabBarController.selectedIndex == 1)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-2"];
    }
    else if (tabBarController.selectedIndex == 2)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-3"];
    }
    else if (tabBarController.selectedIndex == 3)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-4"];
    }
    else if (tabBarController.selectedIndex == 4)
    {
        self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-5"];
    }
}

其他提示

I'd set it all up in the main nib/storyboard, but it's easier to show in code. You can create the view controllers in the usual way (again, nib or storyboard or code).

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:@[vc1, vc2, vc3, vc4, vc5] animated:YES];
    UIWindow *window = [[UIApplication sharedApplication] delegate].window;
    [window setRootViewController:tabBarController];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top