【问题标题】:Landscape orientation for UITabBarController?UITabBarController 的横向方向?
【发布时间】:2010-11-25 09:32:10
【问题描述】:

UITabBarController 不允许横向。所以我使用了 UITabBarContoller 的一个子类(称为 RotatingTabBarController)。它的唯一目的是通过向 shouldAutorotateToInterfaceOrientation 调用返回 YES 来允许旋转。

问题是当您在模拟器中旋转 iPhone 时会出现以下 malloc 错误。

malloc: *** error for object 0x3888000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

我在 Snow Leopard 上使用 3.0 SDK 和 Xcode 3.2。我在 malloc_error_break 中设置了一个断点,但我无法将其追溯到我的代码。 我可以做些什么来消除此错误吗?

这里是 RotatingTabBarController 类:

#import <UIKit/UIKit.h>
@interface RotatingTabBarController : UITabBarController {
}
@end 

@implementation RotatingTabBarController
-(BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
@end

更新:

我对一个类别进行了同样的尝试。但它给出了同样的 malloc 错误。

// UITabBarController+Rotation.h
@interface UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end

// UITabBarController+Rotation.m
#import "UITabBarController+Rotation.h"

@implementation UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   return YES;
}
@end

回溯

[Session started at 2009-09-05 12:13:19 -0400.]
Untitled(992,0xa06d9500) malloc: *** error for object 0x2024000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Untitled(992,0xa06d9500) malloc: *** error for object 0x2014000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

[Session started at 2009-09-05 12:13:27 -0400.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1344) (Fri Jul  3 01:19:56 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".Attaching to process 992.
sharedlibrary apply-load-rules all
(gdb) bt
#0  0x951908fa in mach_msg_trap ()
#1  0x95191067 in mach_msg ()
#2  0x30244d62 in CFRunLoopRunSpecific ()
#3  0x30244628 in CFRunLoopRunInMode ()
#4  0x32044c31 in GSEventRunModal ()
#5  0x32044cf6 in GSEventRun ()
#6  0x309021ee in UIApplicationMain ()
#7  0x00002608 in main (argc=1, argv=0xbfffef94) at /Users/vishwas/Desktop/Untitled/main.m:13
(gdb) 

【问题讨论】:

  • 我们能够通过使用 UITabBarController 上的类别而不是子类来使其旋转,这对我们有用。我不确定如何处理您的错误,因为在这种情况下我还没有遇到它:(
  • 你能解释一下如何进行分类实现吗?
  • 我尝试过这样的 Kevlar 方法,但我仍然得到相同的 malloc 指针释放错误。 :( // UITabBarController+Rotation.h @interface UITabBarController (rotation) - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; @end // UITabBarController+Rotation.m #import "UITabBarController+Rotation.h" @implementation UITabBarController (rotation) - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } @end
  • 抱歉上面的格式。我没有意识到你不能在评论部分格式化代码。

标签: iphone uitabbarcontroller landscape


【解决方案1】:

这是 iPhone SDK 3.0 中的一个错误。它已在 iPhone SDK 3.1 中修复

【讨论】:

    【解决方案2】:

    子类化 UITabBarController 不是获得界面旋转的推荐方式。事实上,Apple 的文档严格规定不要继承 UITabBarController 或 UINavigationController。 它还说,为了让 UITabBarController 支持自动旋转,由它“管理”的所有控制器都必须支持这种方向(即在这种情况下为横向)。

    【讨论】:

    • 感谢您的回答。我一直在转圈,直到我读到它。我的一个观点还没有完成,当然也没有返回“YES”。
    • 相关:旋转对我有用(使用苹果批准的让所有“托管”控制器返回 YES 的方法),但我的视图在旋转时没有自动调整大小,尽管有所有调整大小的属性在界面生成器中正确设置。这个金块帮助了我:codebunnygames.com/?p=18
    【解决方案3】:

    该错误看起来像是在没有分配/初始化的情况下发布了某些东西,或者它正在被双重发布,这看起来不像是您的子类或代码中的错误。

    我喜欢 Kevlar 的类别方法来解决您的问题,它富有创意、简单,应该适合您。

    【讨论】:

    • 我已经请 Kevlar 解释如何进行类别实现。
    • 如果您尝试了类别方法但它不起作用,那就更能证明问题不在于您的子类,而在于其他地方。检查你的 alloc/init/release 周期,确保你没有重复释放
    • 这是一个简单的项目。在 Xcode 中,我创建了一个新的测试项目作为“标签栏应用程序”。将两个 UIBarTabBarController+Rotation.h & .m 文件添加到项目中,并将 #import "UITabBarController+Rotation.h" 行添加到 TestAppDelegate.h。就是这样,项目中没有其他代码,但它仍然给出这个错误。
    • 当您收到错误时,进入 GDB 控制台并键入 backtrace 以获取堆栈转储
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多