委托方法中的应用程序崩溃

委托方法中的应用程序崩溃

本文介绍了UITabBarController 委托方法中的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我正在尝试动态添加和删除标签栏元素.有两个数组.第一个显示的是添加的名为More"的 tabbaritem,当用户按下 More 时,其他数组被添加到 tabbar.用户可以通过按第二个数组中的 Less tabbaritem 返回到第一个数组.问题是,当我经常按更多和更少的 tabbaritems More、Less、More、Less、More、Less 时 - 应用程序在最后一个 Less 之后崩溃.Array 对我来说似乎没问题,tabbar 控制器也是如此.我无法弄清楚问题所在.下面是标签栏委托方法的代码.

HiI am trying to add and remove tab bar elements dynamically. There are two arrays. One is shown first with an added tabbaritem with name "More" and other array is added to the tabbar when user presses More. User can come back to first array by pressing Less tabbaritem in second array. Problem is that when i frequently press More and Less tabbaritems in sequence More, Less, More, Less, More, Less - The app crashes after last Less. Array seems ok to me and so is tabbar controller. I am not able to figure out the problem.Below is the code of tab bar delegate method.

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
 NSLog(@"selected view controller is :%@",viewController);
 if(viewController.view.tag == -1){
     [self.tabBarController setViewControllers:self.level2TabBarItems animated:YES];
     [self.tabBarController setSelectedIndex:0];
 }else if(viewController.view.tag == -2){
     [self.tabBarController setViewControllers:self.level1TabBarItems animated:YES];
     [self.tabBarController setSelectedIndex:0];
 }

}

谁能告诉我我哪里做错了?最好的问候

Can anyone please let me know where I am doing wrong?Best Regards

推荐答案

我遇到了类似的问题.我猜您是在数组中构造了新的 VC 实例,因此频繁切换更多/更少会导致从旧实例调用方法(当时尚未替换).

I had similar problem. I guess that you construct new instance of VC in your array, so frequently switching more/less causes calling method from the old instance (is not replaced yet at that moment).

不幸的是 setViewControllers 方法(如 documentation 说)自动删除调用 dealloc 的旧视图控制器,似乎没有其他方法重复使用它们.

Unfortunatelly setViewControllers method (as documentation say) automatically remove old view controllers calling dealloc and it seems that there is no other way to reuse them.

在您的情况下,您可以尝试禁用选择选项卡,直到 tabBarController:didSelectViewController: 执行实施(我没有测试):

In your case you can try to disable selecting tabs until tabBarController:didSelectViewController: execute implementing (I didn't test it):

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    self.selectLock = YES;
    // your code
    self.selectLock = NO;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return !self.selectLock;
}

这篇关于UITabBarController 委托方法中的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:14