gpt4 book ai didi

iphone - 自定义标签栏 Controller 不工作?

转载 作者:行者123 更新时间:2023-11-29 11:16:19 24 4
gpt4 key购买 nike

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

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

就像我说的,这将正确显示所选的 Controller ,但是当应用程序启动并且我尝试使用选项卡栏切换 View 时, subview 只是变成灰色......我一直在浏览几个教程试图弄清楚解决了这个问题,但似乎我做的完全一样。我还检查了 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;
}
}

最佳答案

试试这个

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

关于iphone - 自定义标签栏 Controller 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9271474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com