ipad objective c using removeFromSuperview to remove UICollectionViewController throws an error

拈花ヽ惹草 提交于 2019-12-25 04:14:28

问题


So I'm customizing this control I found since I think it works very well except with this issue of mine:

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

I wanted to load a UICOllectionViewCOntroller instead of a regular ViewController whenever an item is tapped on the sidebar. So I did this modification when selecting an item:

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

So what I did is since it already handles the index number of the items on teh sidebar, I just made sure it instantiates the type of controller it needs to load using this line, I have 3 regular VC and 1 collection VC:

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

This is how it looks like:

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

Now everything would load smoothly, but whenever I would go to the Collection VC tab and then move away from it by going to another tab it would throw this error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

The application bombs on this part when I remove it from the superview:

[previousViewController.view removeFromSuperview];

Was wondering why it would instantiate the UIView again when all I'm doing is removing it from the stack (is that the right term?)

EDIT: Added some more codes


回答1:


So I finally figured it out and hopefully someone else can find this useful. When generating a UiCollectionView you need to initiate the Layout for some reason. Idk why, I will try to find out. But this is what led me to the solution:

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

Before when I instantiated the CollectionViewController on the main ViewController before the FSVerticalTabbar is called I just used the class connected to the ViewController on the storyboard eg. AdminMainController, AdminEventsCollectionController, etc.

I basically just added the Layout and used that for the UICollectionViewController when being initiated. It now removes the VC without any errors.

UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
 [aFlowLayout setItemSize:CGSizeMake(200, 140)];
 [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
myCollectionViewController = [[MyCollectionViewController alloc]initWithCollectionViewLayout:flowLayout];


来源:https://stackoverflow.com/questions/15490656/ipad-objective-c-using-removefromsuperview-to-remove-uicollectionviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!