簡體   English   中英

以編程方式在appdelegate中創建tabBarController

[英]Programmatically create tabBarController in appdelegate

我一直在關注如何以編程方式添加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];

這對我的名為MenuViewController rootViewController沒有任何作用。 我怎樣才能做到這一點?

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

您應該將標簽欄控制器添加為根視圖控制器:

[self.window setRootViewController:tabBarController];

首先將對象添加到數組中然后用它做一些事情(另一種方式)是個好主意:

[tabs addObject:navController];
[tabBarController setViewControllers:tabs];
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];

如果您想將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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM