所以我正在定制我发现的这个控件,因为我认为它工作得很好,除了我的这个问题:
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(这是正确的术语吗?)
编辑:添加了更多代码