繁体   English   中英

第二个选项卡在UITabbarController中始终显示为灰色

[英]second Tab is always greyed out in UITabbarController

我正在尝试使用应用程序委托中的以下代码将两个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

并编译并运行。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM