不是从 appDelegate 添加 UITabBarController 吗?

发布于 2024-11-25 00:18:07 字数 1032 浏览 1 评论 0原文

是只有我一个人还是我在网上看到的 uitabbarcontroller 的所有示例都需要修改 appDelegate?如果标签栏稍后出现在应用程序中并且之前有一些其他屏幕怎么办?有人可以解释一下这一点或指出应用程序的第一个屏幕不是选项卡栏视图的示例吗?我对此感到抓狂,如果不是来自 AppDelegate,似乎无法让选项卡栏工作。

谢谢!!!

更新:

所以我在我的一个视图控制器中使用一种方法执行此操作,一旦您单击按钮移动到下一个屏幕(即选项卡栏视图),该方法就会触发。它在执行过程中崩溃:

  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]; //CRASHES HERE
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

知道为什么吗?谢谢!!

它在这一行崩溃:

self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; //CRASHES HERE

Is it just me or literally ALL the examples that I have seen on the web for uitabbarcontroller require modifying the appDelegate? What if the tabbar comes later on in the app and there are some other screens before? Can someone explain this or point to an example where the first screen of the app is NOT a tabbar view? I am getting nuts from this and cant seem to be able to have the tabbar to work if not from the AppDelegate.

Thanks!!!

UPDATE:

So I am doing this in one of my view controllers in a method that triggers once you click on a button to move to the next screen (which is the tab bar view). It crashes during the execution:

  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]; //CRASHES HERE
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

Any idea why? Thanks!!

It crashes in this line:

self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; //CRASHES HERE

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

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

发布评论

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

评论(3

稚然 2024-12-02 00:18:07

您可以将UITabBarController从任何视图控制器推送到导航控制器。这样做可能会折叠导航栏,因为 UITabBarController 中的视图控制器本身可能包含 UINavigationController

为了解决这个导航栏问题,您需要从推送选项卡栏控制器的视图控制器中隐藏当前导航控制器的导航栏。在当前视图控制器的 viewWillDisappear: 方法中隐藏导航栏。

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

You can push the UITabBarController to the navigation controller from any view controller. Doing so may collapse the navigation bars as the view controllers in UITabBarController may themselves contain UINavigationController.

In order to overcome this navigation bar issue, you need to hide the navigation bar of the current navigation controller from the view controller where you push the tab bar controller. Hide the navigation bar in viewWillDisappear: method of the current view controller.

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
会傲 2024-12-02 00:18:07

好的,我们开始吧。假设mainWindow.xib有1个UINavigationController和1个TabBarController。前几个屏幕是用导航控制器设计的,稍后当您想要 tabBarController 时,您需要做的就是从 mainWindow 中删除 navigationController 的视图并将 tabBarController 的视图添加为窗口的子视图。如果不告诉我,我希望你能理解。

Ok here we go. Suppose mainWindow.xib has one UINavigationController and one TabBarController. First few screens are deisgned with navigation controller later at some point when you want tabBarController what you need to do is just remove the navigationController's view from mainWindow and add tabBarController's view as a subview to window. I hope you understand if not let me know.

你没皮卡萌 2024-12-02 00:18:07

由于 UITabBarController 类继承自 UIViewController 类,因此选项卡栏控制器拥有自己的视图,可通过 view 属性访问该视图。部署选项卡栏界面时,必须将此视图安装为窗口的根目录。与其他视图控制器不同,标签栏界面永远不应该安装为另一个视图控制器的子级。

http://developer.apple.com/库/ios/#DOCUMENTATION/UIKit/Reference/UITabBarController_Class/Reference/Reference.html

Because the UITabBarController class inherits from the UIViewController class, tab bar controllers have their own view that is accessible through the view property. When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITabBarController_Class/Reference/Reference.html

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