当 UITabBar 不是 rootViewController 时,如何以编程方式 link UITabBar 与带有 NIB 的不同 ViewController

How to programmatically link UITabBar with Different ViewControllers with NIBs when UITabBar is NOT rootViewController

我有一个 tableViewController,一旦点击一个单元格,它就会带你到 FirstViewControllerFirstViewController 有一个包含 4 个项目的 UITabBar。我想以编程方式将每个 buttons/items 挂钩到我的项目中已经存在的 viewController 。

我做了很多研究,但还没有找到解决这个问题的方法

到目前为止我得到的是FirstViewController, SecondViewController, ThirdViewController, FourthViewController

FirstViewController.xib 我有带 4 个图标的 TabBar。

FirstViewController.h

@property (strong, nonatomic) IBOutlet UITabBarController *tabBarController;
@property (weak, nonatomic) IBOutlet UITabBarItem *tabBar;
@property (weak, nonatomic) IBOutlet UITabBarItem *firstItem;
@property (weak, nonatomic) IBOutlet UITabBarItem *secondItem;
@property (weak, nonatomic) IBOutlet UITabBarItem *thirdItem;
@property (weak, nonatomic) IBOutlet UITabBarItem *fourthItem;

FirstViewController.m

- (void)viewDidLoad {
    FirstViewController *first = [[FirstViewController alloc] init];
    SecondViewController *second = [[SecondViewController alloc] init];
    ThirdViewController *third = [[ThirdViewController alloc] init];
    FourthViewController *fourth = [[FourthViewController alloc] init];

    self.tabBarController.viewControllers = [NSArray arrayWithObject: first, second, third, fourth, nil];

}

当我在第四个按钮上按 Tab 键时此代码不起作用,例如它不显示 fourthViewController 笔尖

假设每个 UIViewController 子类都有一个关联的 .xib (nib) 然后使用指定初始化程序。

而不是[[FirstViewController alloc] init],对每个视图控制器使用initWithNibName:bundle:

例子-

[[FirstViewController alloc] initWithNibName:@"nameOfNibFile" bundle:nil];