自定义标签栏控制器不起作用?

发布于 2025-01-05 16:51:49 字数 2470 浏览 2 评论 0原文

我正在尝试构建自定义选项卡栏控制器,但由于某种原因,视图不会切换...初始视图已正确加载。这是我的初始化方法:

- (id)initWithNibName:(NSString *)nibNameOrNil 
           bundle:(NSBundle *)nibBundleOrNil
{
AccountViewController *accountViewController = [[AccountViewController alloc]
                    initWithNibName:@"AccountViewController" bundle:nil];
MoreViewController *moreViewController = [[MoreViewController alloc]
                    initWithNibName:@"MoreViewController" bundle:nil];
BarTabViewController *barTabViewController = [[BarTabViewController alloc]
                    initWithNibName:@"BarTabViewController" bundle:nil];
LocationsViewController *locationsViewController = [[LocationsViewController alloc]
                    initWithNibName:@"LocationsViewController" bundle:nil];

self.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
                        barTabViewController, moreViewController, nil];

[self.view addSubview:locationsViewController.view];
self.selectedController = locationsViewController;

    return self;
}

就像我说的,这将正确显示选定的控制器,但是当应用程序启动并且我尝试使用选项卡栏切换视图时,子视图就会变成灰色......我一直在浏览几个教程试图弄清楚这个问题,但似乎我正在做同样的事情。我还检查了 IB 文件以确保我的选项卡连接正确,确实如此。以下是切换项目的代码:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item == locationsTabBarItem) {
        UIViewController *locationsController = [viewControllers objectAtIndex:0];
        [self.selectedController.view removeFromSuperview];
        [self.view addSubview:locationsController.view];
        self.selectedController = locationsController;
    }
    else if (item == accountsTabBarItem) {
        UIViewController *accountsController = [viewControllers objectAtIndex:1];
        [self.selectedController.view removeFromSuperview];
        [self.view addSubview:accountsController.view];
        self.selectedController = accountsController;
    }
    else if (item == barTabTabBarItem) {
        UIViewController *barTabController = [viewControllers objectAtIndex:2];
        [self.selectedController.view removeFromSuperview];
        [self.view addSubview:barTabController.view];
        self.selectedController = barTabController;
    }
    else {
        UIViewController *moreController = [viewControllers objectAtIndex:3];
        [self.selectedController.view removeFromSuperview];
        [self.view addSubview:moreController.view];
        self.selectedController = moreController;
    }
}

I am trying to build a custom tab bar controller but for some reason the views will not switch... The initial view is loaded properly. Here is my init method:

- (id)initWithNibName:(NSString *)nibNameOrNil 
           bundle:(NSBundle *)nibBundleOrNil
{
AccountViewController *accountViewController = [[AccountViewController alloc]
                    initWithNibName:@"AccountViewController" bundle:nil];
MoreViewController *moreViewController = [[MoreViewController alloc]
                    initWithNibName:@"MoreViewController" bundle:nil];
BarTabViewController *barTabViewController = [[BarTabViewController alloc]
                    initWithNibName:@"BarTabViewController" bundle:nil];
LocationsViewController *locationsViewController = [[LocationsViewController alloc]
                    initWithNibName:@"LocationsViewController" bundle:nil];

self.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
                        barTabViewController, moreViewController, nil];

[self.view addSubview:locationsViewController.view];
self.selectedController = locationsViewController;

    return self;
}

Like I said, this will display the selected controller properly, however when the app launches and I try to switch views with the tab bar, the subview just become grey... I have been looking through several tutorials to try to figure out this issue, but it seems that I am doing it exactly the same. I have also checked the IB file to make sure my tabs are connected properly, they are. The following is the code to switch items:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item == locationsTabBarItem) {
        UIViewController *locationsController = [viewControllers objectAtIndex:0];
        [self.selectedController.view removeFromSuperview];
        [self.view addSubview:locationsController.view];
        self.selectedController = locationsController;
    }
    else if (item == accountsTabBarItem) {
        UIViewController *accountsController = [viewControllers objectAtIndex:1];
        [self.selectedController.view removeFromSuperview];
        [self.view addSubview:accountsController.view];
        self.selectedController = accountsController;
    }
    else if (item == barTabTabBarItem) {
        UIViewController *barTabController = [viewControllers objectAtIndex:2];
        [self.selectedController.view removeFromSuperview];
        [self.view addSubview:barTabController.view];
        self.selectedController = barTabController;
    }
    else {
        UIViewController *moreController = [viewControllers objectAtIndex:3];
        [self.selectedController.view removeFromSuperview];
        [self.view addSubview:moreController.view];
        self.selectedController = moreController;
    }
}

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

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

发布评论

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

评论(1

梦幻的心爱 2025-01-12 16:51:49

尝试用这个

 self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.selectedIndex = 0;

    self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
                        barTabViewController, moreViewController, nil];
    self.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self.navigationController pushViewController:delegate.tabBarController animated:YES];

try With this one

 self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.selectedIndex = 0;

    self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
                        barTabViewController, moreViewController, nil];
    self.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self.navigationController pushViewController:delegate.tabBarController animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文