简体   繁体   English

如何以编程方式添加UITabBarController(无xib文件或故事板)

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

I want to add a UITabBarController to my application. 我想在我的应用程序中添加一个UITabBarController But I have to do it with code only. 但我必须只用代码来做。 No xib files or storyboards. 没有xib文件或故事板。 How to do this entirely through code? 如何通过代码完全做到这一点?

EDIT: 编辑:

_tbc = [[UITabBarController alloc] init];
aboutUsView = [[AboutUsView alloc] init];
helpView = [[HelpView alloc] init];
optionsView = [[OptionsView alloc] init];
self.navCon = [[UINavigationController alloc] initWithRootViewController:optionsView];
            [self setnavigationCon:self.navCon];
            [optionsView setdataLayer:self];
if ([navCon.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
      UIImage *image = [UIImage imageNamed:@"Navigation Bar_reduced.png"];
      [self.navCon.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
      [optionsView addSelfView:window];
}
_tbc.viewControllers = [NSArray arrayWithObjects:navCon, aboutUsView, helpView, nil];
[window addSubview:_tbc.view];

Try this 尝试这个

AppDelegate.h

@interface AppDelegate : UIResponder <UITabBarControllerDelegate>
@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 ICON FOR TABBAR

In your ViewController.m file do as follow: 在ViewController.m文件中,执行以下操作:

- (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;
}

Add this code in your AppDelegate.m 在AppDelegate.m中添加此代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

   FirstViewController * fvc=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];

   SecondViewController * svc=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];


   ThirdViewController * tvc=[[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];

   FourthViewController * fvc2=[[FourthViewController alloc]initWithNibName:@"FourthViewController" bundle:nil];


   tabbar=[[UITabBarController alloc]init];

   tabbar.viewControllers=[NSArray arrayWithObjects:fvc,svc,tvc,fvc2,nil];

   [self.window addSubview:tabbar.view];
}

Here is the code from user1673099 in Swift (nice username btw... ;0) - 以下是Swift中user1673099的代码(很好的用户名btw ...; 0) -

func initialize_tabs() {

    var ncArr : [UINavigationController] = [UINavigationController]();

    for _ in 0...3 {

        let nc : UINavigationController = UINavigationController();
        let vc = UIViewController();

        nc.viewControllers = [vc];

        let v : UIView = UIView(frame: UIScreen.mainScreen().bounds);

        let redC   : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
        let greenC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
        let blueC  : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);

        v.backgroundColor = UIColor(red: redC, green: greenC, blue: blueC, alpha: 1);

        let l : UILabel = UILabel(frame: UIScreen.mainScreen().bounds);
        l.text = "Test Label";
        l.textAlignment = .Center;
        v.addSubview(l);

        vc.view = v;

        ncArr.append(nc);
    }

    self.tabBarController = UITabBarController();

    self.tabBarController.viewControllers = ncArr;

    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM