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;
}

我是否必须在 appdelegate 中添加一些内容UniversViewControllerCategoriesViewController或者ProduitsListinTableViewController这直接在 appdelegate 中?

4

2 回答 2

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 {}

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

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

于 2012-11-14T11:29:39.930 回答
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;
}
于 2012-11-14T11:55:22.923 回答