How to add UITabBarController programmatically (no xib file or storyboard)

后端 未结 3 1262
感情败类
感情败类 2020-12-28 20:52

I want to add a UITabBarController to my application. But I have to do it with code only. No xib files or storyboards. How to do this entirely through code?

3条回答
  •  半阙折子戏
    2020-12-28 21:46

    Try this

    AppDelegate.h
    
    @interface AppDelegate : UIResponder 
    @property (strong, nonatomic) UITabBarController *tabBarController;
    
    AppDeleGate.m
    
    
            UINavigationController *nc1;
            nc1 = [[UINavigationController alloc] init];
            [nc1.navigationBar setTintColor:[UIColor blackColor]];
    
    
            UIViewController *viewController1 = [[[FirstScreen alloc] initWithNibName:@"FirstScreen_ipad" bundle:nil] autorelease];
            nc1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
    
            UINavigationController *nc2;
            nc2 = [[UINavigationController alloc] init];
            [nc2.navigationBar setTintColor:[UIColor blackColor]];
            UIViewController *viewController2 = [[[FullList alloc] initWithNibName:@"FullList_ipad" bundle:nil] autorelease];;
            nc2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
    
    
            UIViewController *viewController3 = [[[FavouriteView alloc] initWithNibName:@"FavouriteView_ipad" bundle:nil] autorelease];
            UINavigationController *nc3;
            nc3 = [[UINavigationController alloc] init];
            [nc3.navigationBar setTintColor:[UIColor blackColor]];
    
            nc3.viewControllers = [NSArray arrayWithObjects:viewController3, nil];
    
            UIViewController *viewController4 = [[[UpcomingFights alloc] initWithNibName:@"UpcomingFights_ipad" bundle:nil] autorelease];
            UINavigationController *nc4;
            nc4 = [[UINavigationController alloc] init];
            [nc4.navigationBar setTintColor:[UIColor blackColor]];
    
            nc4.viewControllers = [NSArray arrayWithObjects:viewController4, nil];
    
    
            self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    
            self.tabBarController.viewControllers = [NSArray arrayWithObjects:nc1, nc2,nc3,nc4 ,nil];
    
    
    
     self.window.rootViewController = self.tabBarController;
        [self.window makeKeyAndVisible];
    

    ICON FOR TABBAR

    In your ViewController.m file do as follow:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self.title = NSLocalizedString(@"YOUR View NAME", @"YOUR VIEW NAME");
        self.tabBarItem.image = [UIImage imageNamed:@"YOUR IMAGE NAME"];
     return self;
    }
    

提交回复
热议问题