0

我是xcode的新手,并试图了解UITabBarController的工作原理。我一直在寻找,找不到解决这个问题的直接办法。在我看到的大多数示例/教程中,UITabBarController是在AppDelegate中定义的,然后一旦启动应用程序,就会立即看到选项卡栏。在我的应用程序中,我想先显示一个欢迎屏幕,然后一旦点击“Enter”,就会进入tabbar视图。所以我的对象的理想结构如下所示:TabBar/UITabBarController实现的选项

MyProjectAppDelegate - > MyProjectViewController - >的firstView/SecondView

据我了解,有关什么的TabBar然后应在MyProjectAppDelegate宣称这种结构。我尝试着看一些UITabBarController在AppDelegate中声明并在MyProjectViewController中执行相同的例子,但没有任何反应。

例如,我连接到“输入”的UIButton我的欢迎屏幕上的IBAction为内做这在我的MyProjectViewController:

- (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];  
} 

同样,这也没做什么,一旦我点击了“输入“按钮,即使它在我从它拿起它的例子中(它在AppDelegate中的位置)执行作业

我也在我的MyProjectViewController上试过这个,其中tabbar确实显示在第一个/第二个视图上,但没有选择自定义它(只是空白的黑条没有任何关于它们,不知道在哪里配置它们):

- (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]; 

} 

这里出了什么问题,应该怎么做才是正确的做法?一个快速的例子将不胜感激。

谢谢!

回答

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]; 
} 
+0

我试图避免使用的通知,并改变了我的AppDelegate太多(我可能有几个标签栏以后和不想要弄糟了应用程序委托 - 更喜欢更模块化的设计)。尽管谢谢! – TommyG