以编程方式在appdelegate中创建tabBarController

时间:2014-02-25 10:18:20

标签: ios objective-c uitabbarcontroller

我一直在关注如何以编程方式添加UITabBarController的不同教程。使用故事板很容易实现,但是因为我正在尝试以编程方式学习如何做事,所以我不能这样做。

目前,我已在didFinishLaunchingWithOptions

中获得此代码
tabBarController = [[UITabBarController alloc] init];

NSMutableArray *tabs = [[NSMutableArray alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[[MenuViewController alloc] init]];

[tabBarController setViewControllers:tabs];

[tabs addObject:navController];


[self.window addSubview:tabBarController.view];

已编辑的代码:

tabBarController = [[UITabBarController alloc] init];

MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navController];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];

这对rootViewController MenuViewController {{1}}无效。我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:8)

用于5选项卡UITabbarcontroller的波纹管代码尝试使用以下波纹管代码: -

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

anasayfaViewController * firstTab= [[anasayfaViewController alloc] initWithNibName:@"anasayfaViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];

SehirRehberiViewController *sehirRehberi = [[SehirRehberiViewController alloc] initWithNibName:@"SehirRehberiViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:sehirRehberi];

duyuruViewController *duyuru = [[duyuruViewController alloc] initWithNibName:@"duyuruViewController" bundle:nil]; 
UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:duyuru];

sikayetViewController *sikayet = [[sikayetViewController alloc] initWithNibName:@"sikayetViewController" bundle:nil];
UINavigationController *navigationController4 = [[UINavigationController alloc] initWithRootViewController:sikayet];

digerViewController *diger = [[digerViewController alloc] initWithNibName:@"digerViewController" bundle:nil];
UINavigationController *navigationController5 = [[UINavigationController alloc] initWithRootViewController:diger];


self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navigationController1,navigationController2,navigationController3,navigationController4,navigationController5];

 [self.window setRootViewController:tabBarController];
 [self.window makeKeyAndVisible];

答案 1 :(得分:1)

您应该将标签栏控制器添加为根视图控制器:

[self.window setRootViewController:tabBarController];

最好先将对象添加到数组中,之后再对其进行操作(反过来):

[tabs addObject:navController];
[tabBarController setViewControllers:tabs];

答案 2 :(得分:1)

UIViewController *viewController_favorites = [[[FavoritesViewController alloc] initWithNibName:@"FavoritesViewController" bundle:nil] autorelease];
UIViewController *viewController_project = [[[ProjectViewController alloc] initWithNibName:@"ProjectViewController" bundle:nil] autorelease];
UIViewController *viewController_search = [[[Search alloc] initWithNibName:@"Search" bundle:nil] autorelease];
UIViewController *viewController_setting = [[[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil] autorelease];

UINavigationController *navController_favorite = [[[UINavigationController alloc] initWithRootViewController:viewController_favorites] autorelease];
UINavigationController *navController_project = [[[UINavigationController alloc] initWithRootViewController:viewController_project] autorelease];
UINavigationController *navController_search = [[[UINavigationController alloc] initWithRootViewController:viewController_search] autorelease];


self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController_favorite,navController_project,navController_search,viewController_setting, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

答案 3 :(得分:0)

如果您希望将UITabBarController作为应用程序的rootViewcontroller,则可以将此代码添加到didFinishLaunchingWithOptions函数中。

它添加了一个包含列表控制器和简单视图控制器的导航控制器:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
  UITabBarController*     tabBarController           = [[UITabBarController     alloc] init];
  UITableViewController*  myListController           = [[MyListController       alloc] init];
  UINavigationController* navigationControllerMyList = [[UINavigationController alloc] initWithRootViewController:myListController];
  navigationControllerMyList.tabBarItem              = [[UITabBarItem           alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];

  UIViewController* simpleViewController             = [[SimpleViewController   alloc] init];
  simpleViewController.tabBarItem                    = [[UITabBarItem           alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
  tabBarController.viewControllers                   = @[ navigationControllerMyList , simpleViewController ];

  self.window                    = [[UIWindow alloc] init];
  self.window.rootViewController = tabBarController;
  [self.window makeKeyAndVisible];

  return YES;
}