如何以编程方式添加UITabBarController(无xib文件或情节提要) [英] How to add UITabBarController programmatically (no xib file or storyboard)

查看:71
本文介绍了如何以编程方式添加UITabBarController(无xib文件或情节提要)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中添加一个UITabBarController.但是我只需要用代码来做.没有xib文件或情节提要.如何完全通过代码做到这一点?

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?

_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];

推荐答案

尝试一下

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];

TABBAR图标

在ViewController.m文件中,请执行以下操作:

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

这篇关于如何以编程方式添加UITabBarController(无xib文件或情节提要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆