我试图将两个NavBar控制器嵌入到UITabbarController中,使用app delegate中的以下代码:
- (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中),并且每个视图都显示正常(在第一个选项卡中)。
app委托不实现UITabbarDelegate和uiTabBarControllerDelegate。我不会使用故事板。
为什么第二个标签总是灰显的任何明显原因?
示例代码: 它只是Apple的位置教程(使用ARC) here
然后改变:
代表
- (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 by
@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
并编译并运行。
答案 0 :(得分:0)
尝试摆脱[window addSubview:self.tabBarController.view];