UITabBar – setItems:动画:崩溃

发布于 2024-10-03 06:25:52 字数 2779 浏览 2 评论 0原文

所以我有一个在界面生成器中创建的 UITabView 控制器。 UITabBarItem 的标题和图像属性在 IB 中设置。 xib 中存在选项卡栏控制器对象,并且已建立所有必要的连接。我可以调用简单的命令,例如

[tabBarController setSelectedIndex:1];

一切工作正常,但是当我设置“items”属性时,我崩溃了。也许我遇到了内存管理问题?

以下是崩溃代码:

NSMutableArray *modifiedItems = [[tabBarController.tabBar items] mutableCopy];  
[modifiedItems removeObjectAtIndex:2];  
NSArray *newItems = [[NSArray alloc] initWithArray:modifiedItems];  
-->[tabBarController.tabBar setItems:newItems animated:NO];  

* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“不允许直接修改由选项卡栏控制器管理的选项卡栏。” * 第一次抛出时的调用堆栈:

0   CoreFoundation                      0x02b9bb99 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x02ceb40e objc_exception_throw + 47
2   CoreFoundation                      0x02b54238 +[NSException raise:format:arguments:] + 136
3   CoreFoundation                      0x02b541aa +[NSException raise:format:] + 58
4   UIKit                               0x005f7019 -[UITabBar setItems:animated:] + 2037
5   Zag Map                             0x00003422 -[ZagMapAppDelegate iPodTouchRemoval] + 270
6   Zag Map                             0x00002eea -[ZagMapAppDelegate applicationDidFinishLaunching:] + 551
7   UIKit                               0x003faf80 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1252
8   UIKit                               0x003fd3b0 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 346
9   UIKit                               0x004073ec -[UIApplication handleEvent:withNewEvent:] + 1958
10  UIKit                               0x003ffb3c -[UIApplication sendEvent:] + 71
11  UIKit                               0x004049bf _UIApplicationHandleEvent + 7672
12  GraphicsServices                    0x03357822 PurpleEventCallback + 1550
13  CoreFoundation                      0x02b7cff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
14  CoreFoundation                      0x02add807 __CFRunLoopDoSource1 + 215
15  CoreFoundation                      0x02adaa93 __CFRunLoopRun + 979
16  CoreFoundation                      0x02ada350 CFRunLoopRunSpecific + 208
17  CoreFoundation                      0x02ada271 CFRunLoopRunInMode + 97
18  UIKit                               0x003fcc6d -[UIApplication _run] + 625
19  UIKit                               0x00408af2 UIApplicationMain + 1160
20  Zag Map                             0x00002ca0 main + 102
21  Zag Map                             0x00002c31 start + 53
    terminate called after throwing an instance of 'NSException'

我的 NSArray 和 NSMutableArray 似乎很好,考虑到它们直接来自正常运行的 UITabBar。它返回三个对象,然后是两个。我希望我只是忽略了一些愚蠢的事情。任何想法我都会非常感激。

So I have a UITabView Controller that was created in interface builder. The title and image properties of UITabBarItem were set in IB. A Tab Bar Controller object is present in the xib and all necessary connections are made. I am able to call simple commands such as

[tabBarController setSelectedIndex:1];

Everything works fine but when I set the 'items' property, I crash. Maybe I am having a memory management problem?

Here is the crashing code:

NSMutableArray *modifiedItems = [[tabBarController.tabBar items] mutableCopy];  
[modifiedItems removeObjectAtIndex:2];  
NSArray *newItems = [[NSArray alloc] initWithArray:modifiedItems];  
-->[tabBarController.tabBar setItems:newItems animated:NO];  

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Directly modifying a tab bar managed by a tab bar controller is not allowed.'
*
Call stack at first throw:

0   CoreFoundation                      0x02b9bb99 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x02ceb40e objc_exception_throw + 47
2   CoreFoundation                      0x02b54238 +[NSException raise:format:arguments:] + 136
3   CoreFoundation                      0x02b541aa +[NSException raise:format:] + 58
4   UIKit                               0x005f7019 -[UITabBar setItems:animated:] + 2037
5   Zag Map                             0x00003422 -[ZagMapAppDelegate iPodTouchRemoval] + 270
6   Zag Map                             0x00002eea -[ZagMapAppDelegate applicationDidFinishLaunching:] + 551
7   UIKit                               0x003faf80 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1252
8   UIKit                               0x003fd3b0 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 346
9   UIKit                               0x004073ec -[UIApplication handleEvent:withNewEvent:] + 1958
10  UIKit                               0x003ffb3c -[UIApplication sendEvent:] + 71
11  UIKit                               0x004049bf _UIApplicationHandleEvent + 7672
12  GraphicsServices                    0x03357822 PurpleEventCallback + 1550
13  CoreFoundation                      0x02b7cff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
14  CoreFoundation                      0x02add807 __CFRunLoopDoSource1 + 215
15  CoreFoundation                      0x02adaa93 __CFRunLoopRun + 979
16  CoreFoundation                      0x02ada350 CFRunLoopRunSpecific + 208
17  CoreFoundation                      0x02ada271 CFRunLoopRunInMode + 97
18  UIKit                               0x003fcc6d -[UIApplication _run] + 625
19  UIKit                               0x00408af2 UIApplicationMain + 1160
20  Zag Map                             0x00002ca0 main + 102
21  Zag Map                             0x00002c31 start + 53
    terminate called after throwing an instance of 'NSException'

My NSArray and NSMutableArray appear to be fine considering they came straight out of the functioning UITabBar. It returned three objects, then two. I hope I'm just overlooking something stupid here. Any ideas I would really appreciate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下雨或天晴 2024-10-10 06:25:52

异常消息准确地告诉您出了什么问题:

“不允许直接修改由选项卡栏控制器管理的选项卡栏。”

设置标签栏控制器的 viewControllers 属性。

The exception message tells you exactly what is wrong:

'Directly modifying a tab bar managed by a tab bar controller is not allowed.'

Set the tab bar controller's viewControllers property instead.

再见回来 2024-10-10 06:25:52
NSMutableArray *viewControllersCopy = [[tabBarController viewControllers] mutableCopy];
[viewControllersCopy removeObjectAtIndex:2];
NSArray *modifiedViewControllers = [[NSArray alloc] initWithArray:viewControllersCopy];
[tabBarController setViewControllers:modifiedViewControllers animated:NO];
NSMutableArray *viewControllersCopy = [[tabBarController viewControllers] mutableCopy];
[viewControllersCopy removeObjectAtIndex:2];
NSArray *modifiedViewControllers = [[NSArray alloc] initWithArray:viewControllersCopy];
[tabBarController setViewControllers:modifiedViewControllers animated:NO];
内心旳酸楚 2024-10-10 06:25:52

你可以这样做——

 tabBarController.selectedIndex = 1; // set index according to your requirement

You can do like that-

 tabBarController.selectedIndex = 1; // set index according to your requirement
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文