iphone - 如何以编程方式添加 UITabBarController(无 xib 文件或 Storyboard)

标签 iphone ios uitabbarcontroller

我想在我的应用程序中添加一个 UITabBarController。但我必须只用代码来做。没有 xib 文件或 Storyboard。如何完全通过代码来做到这一点?

编辑:

_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 图标

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

关于iphone - 如何以编程方式添加 UITabBarController(无 xib 文件或 Storyboard),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18826125/

相关文章:

objective-c - 由于 NSMaxRange(indexRange) <= [self.dataSource numberOfRecordsForPlot :self] 无法绘制

objective-c - 如何以编程方式触发方法 "tabBarController:didSelectViewController:"?

swift - 通过远程通知从 AppDelegate 推送到 UINavigationController

swift - 在 Swift 或 Storyboard 中按下标签栏时如何以模态方式呈现 View Controller ?

iphone - 两个坐标与 CoreLocation 之间的距离

iphone - dispatch_queue_create 具有相同标签的多个调用

ios - 在完成 iPhone 部分后实现 iPad .xib

iphone - NSDateFormatter 关闭一小时

ios - 如何在 xcode 中以编程方式向按钮添加操作

ios - 访问 UIPageViewController 手势识别器以启用滑动删除