TabBar/UITabBarController 实现的选项

发布于 2024-11-24 23:43:50 字数 2041 浏览 1 评论 0原文

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

MyProjectAppDelegate --> MyProjectViewController --> FirstView / SecondView

据我了解,不应使用此结构在 MyProjectAppDelegate 中声明任何与 tabbar 相关的内容。我尝试查看一些在 AppDelegate 中声明 UITabBarController 的示例,并在 MyProjectViewController 中执行相同的操作,但没有任何反应。

例如,我在 MyProjectViewController 中的 IBAction 中执行了此操作,该 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 上尝试了此操作,其中选项卡栏确实显示在第一个/第二个视图上,但没有选项对其进行自定义(只是空白的黑条)上面什么也没有,也不知道在哪里配置它们):

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

}

这里出了什么问题,正确的方法应该是什么?一个简单的例子将受到高度赞赏。

谢谢!

I am new to xcode and trying to understand how UITabBarController works. I have been looking everywhere and could not find a straight solution to this question. In the majority of the examples/tutorials that I see, the UITabBarController is defined in the AppDelegate, and then once you launch the app, you see the tab bar right away. In my app, I want to show a welcome screen first, then once you click "Enter" you get to the tabbar view. So the ideal structure of my objects will be the following:

MyProjectAppDelegate --> MyProjectViewController --> FirstView / SecondView

As far as my understanding, nothing tabbar related should then be declared in MyProjectAppDelegate with this structure. I tried to look at some examples where the UITabBarController is declared in the AppDelegate and do the same in the MyProjectViewController, but nothing happens.

For example, I did this in my MyProjectViewController within an IBAction that is connected to the "Enter" UiButton on my welcome screen:

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

Again, this did not do anything once I clicked on the "Enter" button, even though it does the job in the example where I took it from (where it's within the AppDelegate)

I also tried this on my MyProjectViewController, where the tabbar did show up on the First/Second view, but with no option to customize it (just blank black bars with nothing on them and no idea where to configure them):

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

}

What went wrong here and what should be the right way to go about doing that? A quick example would be highly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2024-12-01 23:43:50

我的一个应用程序中有类似的东西。首次启动时,它会显示登录屏幕。用户成功登录后,应用程序切换到选项卡栏控制的视图。

我在我的应用程序代理中进行切换。登录视图发送一个通知,应用程序委托观察并重建屏幕:

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

I have something similar in one of my apps. At first launch it shows a login screen. After the user successfully logs in, the app switches to a tab bar controlled view.

I do the switching in my appdelegate. The login view sends a notification which the app delegate observes and rebuilds the screen:

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文