如何在不使用AppDelegate的情况下以编程方式添加UITabBarController?

我想在两个视图中放置一个UITabBar,但不是在MainViewController中,因为我不使用AppDelegate来放置它。

有没有.xib或故事板的解决scheme

UIViewController *vc1 = [[UIViewController alloc] init]; vc1.title = @"FIRST"; vc1.view.backgroundColor = [UIColor blueColor]; UIViewController *vc2 = [[UIViewController alloc] init]; vc2.title = @"SECOND"; vc2.view.backgroundColor = [UIColor redColor]; UITabBarController *tabBar = [[UITabBarController alloc] init]; tabBar.viewControllers = @[vc1,vc2]; tabBar.selectedIndex = 1; tabBar.view.frame = CGRectMake(50, 50, 220, 320); [tabBar willMoveToParentViewController:self]; [self.view addSubview:tabBar.view]; [self addChildViewController:tabBar]; [tabBar didMoveToParentViewController:self];