【发布时间】:2012-12-20 14:03:13
【问题描述】:
我正在尝试使用应用程序委托中的以下代码将两个 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) here
然后改变:
- (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
然后编译并运行。
【问题讨论】:
-
即使你尝试这个 NSArray *twoViewControllers = [[NSArray alloc]initWithObjects:self.firstNav,self.anotherNav, nil] 是否也会出现同样的问题;
-
是的,同样的问题。 firstNav 转到第一个选项卡,并且显示正常,但无法选择第二个选项卡。确实是第二个选项卡由于某种原因没有响应触摸事件。
-
你能把项目转发给我吗,因为代码似乎没问题。
-
您好,谢谢!由于无法发送私人消息,我不太确定将其发送到哪里,因此我添加了一些有关如何复制此行为的更多信息。
-
sendspace.com。你可以在这里试一试,设置电子邮件到您的个人电子邮件 ID。将生成一个链接,您可以将其粘贴到此处