在应用代理中添加导航栏

时间:2012-11-14 11:27:20

标签: iphone objective-c ios xcode

我的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中?

2 个答案:

答案 0 :(得分: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 {}

您可以在其中设置标签名称和图像。

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

答案 1 :(得分: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;
}