我是xcode的新手,并试图了解UITabBarController的工作原理。我一直在寻找无法找到这个问题的直接解决方案。在我看到的大多数示例/教程中,UITabBarController在AppDelegate中定义,然后一旦启动应用程序,就会立即看到标签栏。在我的应用程序中,我想首先显示欢迎屏幕,然后一旦单击“Enter”,您将进入标签栏视图。所以我的对象的理想结构如下:
MyProjectAppDelegate - > MyProjectViewController - > FirstView / SecondView
据我所知,在MyProjectAppDelegate中使用此结构不应声明任何与tabbar相关的内容。我试着看一些在AppDelegate中声明UITabBarController并在MyProjectViewController中执行相同操作的示例,但没有任何反应。
例如,我在一个IBAction中的MyProjectViewController中执行了此操作,该IBAction连接到欢迎屏幕上的“Enter”UiButton:
- (IBAction) EnterApp {
[window addSubview:tabBarController.view];
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
FirstView* first = [[FirstView alloc] init];
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first];
SecondView* second = [[SecondView alloc] init];
UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:second];
NSArray* controllers = [NSArray arrayWithObjects:firstNav,secondNav, nil];
tabBarController.viewControllers = controllers;
[window addSubview:tabBarController.view];
}
再一次,当我点击“Enter”按钮时,这并没有做任何事情,即使它在我从中获取它的示例(在AppDelegate中的位置)中完成了工作
我也在我的MyProjectViewController上尝试了这个,其中tabbar确实出现在First / Second视图上,但没有选项来自定义它(只是空白的黑条,它们没有任何内容,也不知道在哪里配置它们):< / p>
- (IBAction) EnterApp {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
UIViewController *viewController2 = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}
这里出了什么问题,应该采取什么样的正确方法?一个快速的例子将受到高度赞赏。
谢谢!
答案 0 :(得分:1)
我的某个应用中有类似内容。首次启动时会显示登录屏幕。用户成功登录后,应用程序将切换到标签栏控制视图。
我在我的appdelegate中进行切换。登录视图发送应用程序委托观察并重建屏幕的通知:
- (void)switchView:(NSNotification *)notification {
MyTabbarView *homeView = [[MyTabbarView alloc] init];
NSArray *controllers = [NSArray arrayWithObject:homeView];
[mainNavController setViewControllers:controllers animated:YES];
mainNavController.navigationBar.barStyle = UIBarStyleBlack;
mainNavController.navigationBar.hidden = NO;
[homeView release];
}