与自定义导航控制器崩溃

时间:2013-11-21 23:20:03

标签: objective-c ios7

在iOS7中测试我的应用程序时遇到了一个问题。该应用程序目前在iOS5和iOS6中运行良好。基于堆栈跟踪,看起来私有方法会导致问题。

2013-11-21 17:00:56.565 MyApp[706:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An override of -[UINavigationController navigationBar] is returning an object that is not a kind of UINavigationBar'
*** First throw call stack:
(
    0   CoreFoundation                      0x0244d5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01eee8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x0244d3bb +[NSException raise:format:] + 139
    3   UIKit                               0x00af796c -[UINavigationController _confirmBarAccessMethods] + 277
    4   UIKit                               0x00af7b19 -[UINavigationController initWithNibName:bundle:] + 271
    5   MyApp                               0x000b8bd8 -[CustomUINavigationController initWithNibName:bundle:] + 152
    6   UIKit                               0x00af77b3 -[UINavigationController initWithRootViewController:] + 86
    7   MyApp                               0x000b8ea1 -[CustomUINavigationController initWithRootViewController:] + 113
    8   MyApp                               0x0000260a -[AppDelegate application:didFinishLaunchingWithOptions:] + 666
    9   UIKit                               0x009c7355 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
    10  UIKit                               0x009c7b95 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1536
    11  UIKit                               0x009cc3a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    12  UIKit                               0x009e087c -[UIApplication handleEvent:withNewEvent:] + 3447
    13  UIKit                               0x009e0de9 -[UIApplication sendEvent:] + 85
    14  UIKit                               0x009ce025 _UIApplicationHandleEvent + 736
    15  GraphicsServices                    0x0478b2f6 _PurpleEventCallback + 776
    16  GraphicsServices                    0x0478ae01 PurpleEventCallback + 46
    17  CoreFoundation                      0x023c8d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    18  CoreFoundation                      0x023c8a9b __CFRunLoopDoSource1 + 523
    19  CoreFoundation                      0x023f377c __CFRunLoopRun + 2156
    20  CoreFoundation                      0x023f2ac3 CFRunLoopRunSpecific + 467
    21  CoreFoundation                      0x023f28db CFRunLoopRunInMode + 123
    22  UIKit                               0x009cbadd -[UIApplication _run] + 840
    23  UIKit                               0x009cdd3b UIApplicationMain + 1225
    24  MyApp                               0x00002337 main + 295
    25  MyApp                               0x00002205 start + 53
)
libc++abi.dylib: terminating with uncaught exception of type NSException

只有在iOS7中进行测试时才会发生此崩溃。一旦调用[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil],就会发生这种情况。我找不到有关_confirmBarAccessMethods的任何信息。我假设它是一个私有的UINavigationController方法。

以下是CustomUINavigationController类的头文件:

#import <UIKit/UIKit.h>
#import "CustomUINavigationBar.h"

@interface CustomUINavigationController : UINavigationController
{
    CustomUINavigationBar *customNavigationBar;
}

@property (nonatomic, retain) UIColor *titleColor;

@end

这是应用程序崩溃的init方法:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];   // This line causes the application to crash
    if (self) {
        customNavigationBar = [[CustomUINavigationBar alloc] init];
        [customNavigationBar setTintColor:[UIColor colorWithWhite:0.0 alpha:1.0]];
        [customNavigationBar setItems:[NSArray array] animated:NO];
        [customNavigationBar setDelegate:self];

        self.titleColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    }
    return self;
}

以下是CustomNavigationBar的标头文件:

#import <UIKit/UIKit.h>

@interface CustomUINavigationBar : UINavigationBar
{
UIImage *background;
}

@end

非常感谢任何帮助。如有必要,我可以提供更多信息。

编辑:从使用自定义导航栏初始值设定项的建议解决方案中添加了跟踪

2013-11-22 13:04:49.756 MyApp[390:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An override of -[UINavigationController navigationBar] is returning an object that is not a kind of UINavigationBar'
*** First throw call stack:
(
    0   CoreFoundation                      0x0244d5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01eee8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x0244d3bb +[NSException raise:format:] + 139
    3   UIKit                               0x00af796c -[UINavigationController _confirmBarAccessMethods] + 277
    4   UIKit                               0x00af7b19 -[UINavigationController initWithNibName:bundle:] + 271
    5   MyApp                               0x000b8e38 -[CustomUINavigationController initWithNibName:bundle:] + 152
    6   UIKit                               0x00af7815 -[UINavigationController initWithNavigationBarClass:toolbarClass:] + 52
    7   MyApp                               0x00002868 -[AppDelegate application:didFinishLaunchingWithOptions:] + 712
    8   UIKit                               0x009c7355 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
    9   UIKit                               0x009c7b95 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1536
    10  UIKit                               0x009cc3a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    11  UIKit                               0x009e087c -[UIApplication handleEvent:withNewEvent:] + 3447
    12  UIKit                               0x009e0de9 -[UIApplication sendEvent:] + 85
    13  UIKit                               0x009ce025 _UIApplicationHandleEvent + 736
    14  GraphicsServices                    0x0478b2f6 _PurpleEventCallback + 776
    15  GraphicsServices                    0x0478ae01 PurpleEventCallback + 46
    16  CoreFoundation                      0x023c8d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    17  CoreFoundation                      0x023c8a9b __CFRunLoopDoSource1 + 523
    18  CoreFoundation                      0x023f377c __CFRunLoopRun + 2156
    19  CoreFoundation                      0x023f2ac3 CFRunLoopRunSpecific + 467
    20  CoreFoundation                      0x023f28db CFRunLoopRunInMode + 123
    21  UIKit                               0x009cbadd -[UIApplication _run] + 840
    22  UIKit                               0x009cdd3b UIApplicationMain + 1225
    23  MyApp                               0x00002567 main + 295
    24  MyApp                               0x00002435 start + 53
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

2 个答案:

答案 0 :(得分:0)

使用initWithNavigationBarClass:toolbarClass:创建导航控制器并提供自定义导航栏类​​。然后在导航控制器超类为您创建的实例上执行所需的任何配置。

答案 1 :(得分:0)

我终于解决了这个问题。 CustomerUINavigationController具有navigationBar访问者的覆盖方法。它试图返回一个零值。我删除了这个,编译并运行的应用程序没有任何问题。