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

标签 iphone ios xcode ipad uitabbarcontroller

我正在尝试构建一个自定义选项卡栏 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/

相关文章:

iphone - UITableViewCell : how to give the built-in imageView a margin on retina display

ios - AFNetworking 将 NSDictionary 作为单个数组值发送,而不是将它们作为完整对象发送

ios - Dispatch_async 停止 UITableView 重新加载

objective-c - 为什么我的异步调用仍然阻止我与 Swift 中的 UI 元素交互?

ios - 如何在 Xcode 中正确使用 Git?

iphone - 在 iOS 中以编程方式进行自动调用

iPhone:在命令行 (Mac OS X) 中压缩 .app 文件会删除 CodeSigning

iphone - 在 iPhone 上构建多 View 应用程序

ios - WatchOS 2 的 Watch Extension 中的 Assets.xcassets

xcode - 哪里可以获得适用于 Xcode 的 macOS SDK 10.6?