0

我正在尝试使用应用程序委托中的以下代码将两个 NavBar 控制器嵌入到 UITabbarController 中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    [window makeKeyAndVisible];

    // Configure and show the window.

    FirstViewController *firstController = [[FirstViewController alloc] initWithNibName:nil bundle:NULL];
    self.firstNav = [[UINavigationController alloc] initWithRootViewController:firstController];
    SecondTableViewController *anotherOne = [[SecondTableViewController alloc] initWithNibName:nil bundle:NULL];
    self.anotherNav = [[UINavigationController alloc] initWithRootViewController:anotherOne];

    NSArray *twoViewControllers = [[NSArray alloc]initWithObjects:self.anotherNav,self.firstNav, nil];

    self.tabBarController = [[UITabBarController alloc]init];
    [self.tabBarController setViewControllers:twoViewControllers];
    [window addSubview:self.tabBarController.view];
    [window setRootViewController:tabBarController];
    return YES;
}   

这两个选项卡显示正常,带有它们的名称,但我只能选择第一个选项卡,第二个选项卡始终显示为灰色并且不响应触摸事件。我将 navController 分配反转到 Tabbar 中(即在数组 twoViewControllers 中),并且每个视图都显示良好(在第一个选项卡中)。

应用程序委托未实现 UITabbarDelegate 和 uiTabBarControllerDelegate。我不使用故事板。

第二个标签总是灰色的有什么明显的原因吗?

示例代码:这里只是 Apple 的 Locations 教程(带有 ARC)

然后改变:

  • (void)applicationDidFinishLaunching:(UIApplication *)application {}

为了

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Configure and show the window.

    RootViewController *cont1 = [[RootViewController alloc] initWithNibName:nil bundle:NULL];
    RootViewController *cont2 = [[RootViewController alloc] initWithNibName:nil bundle:NULL];

    NSManagedObjectContext *context = [self managedObjectContext];
    if (!context) {
        // Handle the error.
    }
    cont1.managedObjectContext = context;
    cont2.managedObjectContext = context;

    UINavigationController *aNav1 = [[UINavigationController alloc] initWithRootViewController:cont1];
    UINavigationController *aNav2 = [[UINavigationController alloc] initWithRootViewController:cont2];

    NSArray *twoViewControllers = [[NSArray alloc]initWithObjects:aNav1,aNav2, nil];

    self.tabBarController = [[UITabBarController alloc]init];
    [self.tabBarController setViewControllers:twoViewControllers];

    //[window addSubview:[tabBarController view]];
    [window setRootViewController:tabBarController];

    [window makeKeyAndVisible];

}

并将@interface LocationAppDelegate ...@end 替换为

@interface LocationsAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;

    UITabBarController *tabBarController;

    NSPersistentStoreCoordinator *persistentStoreCoordinator;
    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;       
}

@property (nonatomic, strong) IBOutlet UIWindow *window;
@property (nonatomic, strong) UITabBarController *tabBarController;

- (IBAction)saveAction:sender;

@property (nonatomic, strong, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@property (weak, nonatomic, readonly) NSString *applicationDocumentsDirectory;

@end

并编译运行。

4

1 回答 1

0

试着摆脱[window addSubview:self.tabBarController.view];

于 2012-12-20T14:26:48.640 回答