2012-11-14 22 views
0

我的appdelegate中存在问题。我想添加一个导航栏,但它不起作用。在应用程序代理中添加导航栏

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    self.tabBarController = [[UITabBarController alloc] init]; 

    UniversViewController *universViewController = [[UniversViewController alloc] initWithNibName:@"ProduitsListinTableViewController" bundle:nil]; 
    universViewController.title = @"univers"; 

    CategoriesViewController *categoriesViewController = [[CategoriesViewController alloc] initWithNibName:@"ProduitsListinTableViewController" bundle:nil]; 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:categoriesViewController]; 
    navigationController.title = @"categories"; 
    [navigationController setNavigationBarHidden:NO]; 

    ProduitsListinTableViewController *produitListe = [[ProduitsListinTableViewController alloc] initWithNibName:@"ProduitsListinTableViewController" bundle:nil]; 
    produitListe.title = @"troisième"; 

    _tabBarController.viewControllers = [NSArray arrayWithObjects:universViewController, categoriesViewController, produitListe, nil]; 

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

    return YES; 
} 

我一定要在UniversViewControllerCategoriesViewControllerProduitsListinTableViewController添加的东西或这是直接在的appdelegate?

回答

0

试试这个代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

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

    // Override point for customization after application launch. 

    UIViewController *viewController1, *viewController2,*viewController3,*viewController4,*viewController5; 

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    { 

     viewController1 = [[[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil] autorelease]; 

     navController=[[UINavigationController alloc] initWithRootViewController:viewController1]; 

     viewController2 = [[[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil] autorelease]; 

     navController2=[[UINavigationController alloc] initWithRootViewController:viewController2]; 

     viewController3 = [[[LocationsViewController alloc] initWithNibName:@"LocationsViewController" bundle:nil] autorelease]; 

     navController3=[[UINavigationController alloc] initWithRootViewController:viewController3]; 

     viewController4 = [[[CallViewController alloc] initWithNibName:@"CallViewController" bundle:nil] autorelease]; 

     navController4=[[UINavigationController alloc] initWithRootViewController:viewController4]; 

     viewController5 = [[[MyBookingsViewController alloc] initWithNibName:@"MyBookingsViewController" bundle:nil] autorelease]; 

     navController5=[[UINavigationController alloc] initWithRootViewController:viewController5]; 


    } 

    self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 

    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController,navController2,navController3,navController4,navController5,nil]; 

    //self.window.rootViewController =self.navController; 

    self.window.rootViewController=self.tabBarController; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 
+0

嗨,谢谢,这是完美的,它的工作原理!我添加了viewController1.title = @“TITLE”;所以标题直接出现。再次感谢你! – hhd59

0

当我同时拥有UINavigationController和UITabbarController时,我总是遵循这种方法:
您需要从基于视图的应用程序开始。然后在你的appDelegate文件中创建一个UITabbarController。

Appdelegate.h 

UITabBarController *tabBarController; 
// set properties 

Appdelegate.m 

// Synthesize 

tabBarController = [[UITabBarController alloc] init]; 
tabBarController.delegate=self; 

// Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController 
Search * search = [[Search alloc] init]; 
UINavigationController *searchNav = [[UINavigationController alloc]  initWithRootViewController:search]; 

Nearby* nearby = [[Nearby alloc] init]; 
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby]; 

Map* map = [[Map alloc] init]; 
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map]; 

AboutUs* aboutUs = [[AboutUs alloc] init]; 
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs]; 

Favorites* favorites = [[Favorites alloc] init]; 
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites]; 

NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil]; 
tabBarController.viewControllers = controllers; 

[window addSubview:tabBarController.view];  

您可以相应地管理在哪个选项卡中放置导航控制器或只有视图控制器。

在上述各你所提到的视图控制器的

然后需要实现

- (id)init {} 

,可以在其中设置选项卡名称和图像。

我总是遵循这种方法,它永远不会失败。标签始终可见。您可以根据您的代码进行更改。

+0

你好,谢谢你的留言。我试过了,但没有奏效。我认为我做了错误的事情。但是,波纹管后为我工作。 – hhd59

+0

@ hhd59你能告诉我它没有工作吗? – Nitish

相关问题