0

所以我正在定制我发现的这个控件,因为我认为它工作得很好,除了我的这个问题:

http://www.cocoacontrols.com/controls/fsverticaltabbarcontroller

每当在侧边栏上点击一个项目时,我想加载一个 UICOllectionViewCONtroller 而不是常规的 ViewController。所以我在选择一个项目时做了这个修改:

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    NSLog(@"selected Index is %@", [NSNumber numberWithInt:selectedIndex]);
    NSLog(@"_selected Index is %@", [NSNumber numberWithInt:_selectedIndex]);
    NSLog(@"vc counts is %i", [self.viewControllers count]);
    if (selectedIndex != _selectedIndex && selectedIndex < [self.viewControllers count])
    {
        // add new view controller to hierarchy
        UIViewController *selectedViewController = [self getSelectedVCWithSelectedIndex:selectedIndex];

        [self addChildViewController:selectedViewController];
        selectedViewController.view.frame = CGRectMake(self.tabBarWidth,
                                                       0,
                                                       self.view.bounds.size.width-self.tabBarWidth,
                                                       self.view.bounds.size.height);
        selectedViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        [self.view addSubview:selectedViewController.view];

        // remove previously selected view controller (if any)
        if (_selectedIndex != NSNotFound)
        {
            UIViewController *previousViewController = [self.viewControllers objectAtIndex:_selectedIndex];
            NSLog(@"ERROR HERE: remove previous: previousVC = %@", previousViewController);
            [previousViewController.view removeFromSuperview];
            [previousViewController removeFromParentViewController];
        }

        // set new selected index
        _selectedIndex = selectedIndex;

        // update tab bar
        if (selectedIndex < [self.tabBar.items count])
        {
            self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:selectedIndex];
        }

        // inform delegate
        if ([self.delegate respondsToSelector:@selector(tabBarController:didSelectViewController:)])
        {
            [self.delegate tabBarController:self didSelectViewController:selectedViewController];
        }
    }
}

所以我所做的是因为它已经处理了侧边栏上项目的索引号,我只是确保它使用这一行实例化它需要加载的控制器类型,我有 3 个常规 VC 和 1 个集合 VC:

UIViewController *previousViewController = [self.viewControllers objectAtIndex:_selectedIndex];

这是它的样子:

-(UIViewController *)getSelectedVCWithSelectedIndex:(NSUInteger)selectedIndex{
    UIViewController *selectedVC = [[UIViewController alloc]init];

    // do a switch case on this.


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    // need to instantiate each and every custom uinav

    switch(selectedIndex){
        case 1:
            selectedVC = [storyboard instantiateViewControllerWithIdentifier:@"UINavAdminCategoryIndexViewControllerID"];
            break;
        case 2:
            selectedVC = [storyboard instantiateViewControllerWithIdentifier:@"UINavAdminParticipantIndexViewControllerID"];
            break;
        case 3:

            selectedVC = [storyboard instantiateViewControllerWithIdentifier:@"UINavAdminTablesIndexCollectionViewControllerID"];
            break;
        case 4:
            selectedVC = [self.viewControllers objectAtIndex:selectedIndex];
            break;
        default:
            break;
    }

    return selectedVC;
}

现在一切都会顺利加载,但是每当我转到Collection VC选项卡然后通过转到另一个选项卡离开它时,它都会抛出此错误:

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“UICollectionView 必须使用非零布局参数初始化”

当我从超级视图中删除它时,应用程序会在这部分爆炸:

[previousViewController.view removeFromSuperview];

想知道为什么当我所做的只是从堆栈中删除它时它会再次实例化 UIView(这是正确的术语吗?)

编辑:添加了更多代码

4

1 回答 1

1

所以我终于想通了,希望其他人能发现这很有用。生成 UiCollectionView 时,出于某种原因,您需要启动 Layout。我知道为什么,我会试着找出答案。但这就是导致我找到解决方案的原因:

http://www.rqna.net/qna/ikvmhu-uicollectionview-must-be-initialized-with-a-non-nil-layout-parameter.html

在调用 FSVerticalTabbar 之前我在主 ViewController 上实例化 CollectionViewController 之前,我只是使用了连接到情节提要上的 ViewController 的类,例如。AdminMainController、AdminEventsCollectionController 等

我基本上只是添加了布局,并在启动时将其用于 UICollectionViewController。它现在删除了 VC,没有任何错误。

UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
 [aFlowLayout setItemSize:CGSizeMake(200, 140)];
 [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
myCollectionViewController = [[MyCollectionViewController alloc]initWithCollectionViewLayout:flowLayout];
于 2013-03-20T05:33:53.330 回答