UINavigationController 不适用于 SearchDisplay 控制器
Posted
技术标签:
【中文标题】UINavigationController 不适用于 SearchDisplay 控制器【英文标题】:UINavigationController not work with SearchDisplay Controller 【发布时间】:2013-12-23 12:36:15 【问题描述】:在我的项目中使用侧边菜单。它是来自 gitHub 的现成模板,但问题是有时 searchDisplay 控制器正常工作,有时会出错,实际问题是什么我不明白......请帮助我
side menu template contain this files:
1)JWSlideMenuController
2)JWNavigationController
3)JWSlideMenuViewController
这里抛出错误
2013-12-22 13:24:22.671 MyProject[3747:13d03] -[JWNavigationController isNavigationBarHidden]: unrecognized selector sent to instance 0x80c4e60
2013-12-22 13:24:22.681 MyProject[3747:13d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[JWNavigationController isNavigationBarHidden]: unrecognized selector sent to instance 0x80c4e60'
*** First throw call stack:
(0x1979012 0x1295e7e 0x1a044bd 0x1968bbc 0x196894e 0x4bf326 0x4c0e41 0x410ae4 0x12a9705 0x1dd213 0x29ec7e 0x29e310 0x2aaa31 0x2b3f5f 0x2a21e9 0x2e7d95 0x4ab3c3 0x4ad442 0x4a485a 0x4a399b 0x4a50df 0x4a7d2d 0x4a7cac 0x49fa28 0x20c972 0x20ce53 0x1ead4a 0x1dc698 0x18d4df9 0x18d4ad0 0x18eebf5 0x18ee962 0x191fbb6 0x191ef44 0x191ee1b 0x18d37e3 0x18d3668 0x1d9ffc 0x5da2 0x1f65)
libc++abi.dylib: terminate called throwing an exception
这是 JWNavigationController.h 和 .m 文件的代码
JWNavigationController.h
#import <UIKit/UIKit.h>
@class JWSlideMenuController;
@interface JWNavigationController : UIViewController <UINavigationBarDelegate>
@property (nonatomic, retain) UINavigationBar *navigationBar;
@property (nonatomic, retain) UIView *contentView;
@property (nonatomic, retain) JWSlideMenuController *slideMenuController;
@property (nonatomic, retain, readonly) UIViewController *rootViewController;
- (id)initWithRootViewController:(UIViewController *)rootViewController;
- (void)pushViewController:(UIViewController *)controller;
- (UIViewController *)popViewController;
@end
JWNavigationController.m 文件
JWNavigationController.m
// JWNavigationController.m
// JWSlideMenu
//
// Created by Jeremie Weldin on 11/22/11.
// Copyright (c) 2011 Jeremie Weldin. All rights reserved.
//
#import "JWNavigationController.h"
#import "JWSlideMenuViewController.h"
@interface JWNavigationController(Private)
-(UIViewController*)removeTopViewController;
@end
@implementation JWNavigationController
@synthesize navigationBar;
@synthesize contentView;
@synthesize slideMenuController;
@synthesize rootViewController=_rootViewController;
#pragma mark - View lifecycle
- (id)init
self = [super init];
if (self)
CGRect masterRect = [[UIScreen mainScreen] bounds];
CGRect contentFrame = CGRectMake(0.0, 44.0, masterRect.size.width, masterRect.size.height - 44.0);
CGRect navBarFrame = CGRectMake(0.0, 0.0, masterRect.size.width, 44.0);
self.view = [[[UIView alloc] initWithFrame:masterRect] autorelease];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.view.backgroundColor = [UIColor whiteColor];
self.contentView = [[[UIView alloc] initWithFrame:contentFrame] autorelease];
self.contentView.backgroundColor = [UIColor whiteColor];
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.contentView];
self.navigationBar = [[[UINavigationBar alloc] initWithFrame:navBarFrame] autorelease];
self.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.navigationBar.delegate = self;
[self.view insertSubview:self.navigationBar aboveSubview:self.contentView];
self.navigationBar.backgroundColor=[UIColor whiteColor];
return self;
- (id)initWithRootViewController:(JWSlideMenuViewController *)rootViewController
self = [self init];
if(self)
_rootViewController = rootViewController;
UIBarButtonItem *menuButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_icon_20x20.png"] style:UIBarButtonItemStyleBordered target:self.slideMenuController action:@selector(toggleMenu)] autorelease];
rootViewController.navigationItem.leftBarButtonItem = menuButton;
[self addChildViewController:rootViewController];
[self.contentView addSubview:rootViewController.view];
[self.navigationBar pushNavigationItem:rootViewController.navigationItem animated:YES];
rootViewController.navigationController = self;
return self;
#pragma mark - UINavigationBarDelegate
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
UIViewController *controller = [self.childViewControllers lastObject];
if (item==controller.navigationItem) //Will now called only if a back button pop happens, not in manual pops
[self removeTopViewController];
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item
#pragma mark - Stack Interaction
- (void)pushViewController:(JWSlideMenuViewController *)controller
[self addChildViewController:controller];
[self.navigationBar pushNavigationItem:controller.navigationItem animated:YES];
controller.navigationController = self;
controller.view.frame = self.contentView.bounds;
if([self.childViewControllers count] == 1)
[self.contentView addSubview:controller.view];
else
UIViewController *previousController = [self.childViewControllers objectAtIndex:[self.childViewControllers count]-2];
[self transitionFromViewController:previousController toViewController:controller duration:0.5 options:UIViewAnimationOptionTransitionNone animations:NULL completion:NULL];
- (UIViewController *)popViewController
//Can use this to pop manually rather than back button alone
UIViewController *controller = [self.childViewControllers lastObject];
UIViewController *previousController = nil;
if([self.childViewControllers count] > 1)
previousController = [self.childViewControllers objectAtIndex:[self.childViewControllers count]-2];
previousController.view.frame = self.contentView.bounds;
[self transitionFromViewController:controller toViewController:previousController duration:0.3 options:UIViewAnimationOptionTransitionNone animations:NULL completion:NULL];
[controller removeFromParentViewController];
if(self.navigationBar.items.count > self.childViewControllers.count)
[self.navigationBar popNavigationItemAnimated:YES];
return controller;
- (void)viewDidUnload
_rootViewController = nil;
self.navigationBar = nil;
self.contentView = nil;
self.slideMenuController = nil;
[super viewDidUnload];
- (void)dealloc
[_rootViewController release];
[navigationBar release];
[contentView release];
[super dealloc];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES;
-(UIViewController*)removeTopViewController
UIViewController *controller = [self.childViewControllers lastObject];
UIViewController *previousController = nil;
if([self.childViewControllers count] > 1)
previousController = [self.childViewControllers objectAtIndex:[self.childViewControllers count]-2];
previousController.view.frame = self.contentView.bounds;
[self transitionFromViewController:controller toViewController:previousController duration:0.3 options:UIViewAnimationOptionTransitionNone animations:NULL completion:NULL];
[controller removeFromParentViewController];
return controller;
@end
如果从 JWNavigationController.m 隐藏此代码,则 searchDisplayController 正在工作 rootViewController.navigationController = self;
但新问题是 UITableVIew didselectrowatindexpath 不起作用.. 我该怎么办?
【问题讨论】:
在 JWNavigationController.h 文件中应该是 UINavigationController 而不是 UIViewController。原因是导航控制器中存在 isNavigationBarHidden。此外,如果您使用的是导航控制器,那么崩溃的原因是您正在使用的 JWNavigationViewController 正在从内存中删除,即您可能正在从内存中删除该对象,或者它正在自动释放。尝试检查以上两个原因,一些解决方案将帮助您摆脱这种情况。 你能帮我做其他你告诉我的事情,但它的显示错误 2013-12-23 05:22:28.161 MyProject[764:13d03] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序',原因:'不能直接在控制器管理的 UINavigationBar 上调用 pushNavigationItem:animated:。' ***第一掷调用堆栈:(0x197a012 0x1296e7e 0x1979deb 0x258362 0x4361 0x11288 0x28a285 0x28a4ed 0xc945b3 0x1939376 0x1938e06 0x1920a82 0x191ff44 0x191fe1b 0x18d47e3 0x18d4668 0x1daffc 0x5de2 0x1fa5)的libc ++ abi.dylib:终止称为抛出异常跨度> 你到底做了什么?可以贴一些代码吗? 另外,请查看***.com/questions/2520912/… 【参考方案1】:isNavigationBarHidden:
是一个选择器,仅在派生自 UINavigationController
的类中可用。你必须修改这一行:
@interface JWNavigationController : UIViewController
用这个:
@interface JWNavigationController : UINavigationController
【讨论】:
【参考方案2】:您的JWNavigationController
不继承自UINavigationController
。
但是isNavigationBarHidden
是UINavigationController
实现的方法。
现在看看错误(它使用术语“选择器”,但它指的是方法):它基本上说你的JWNavigationController
的实例没有实现isNavigationBarHidden
,这就是问题所在。
会不会是你打错字了,打算写:
@interface JWNavigationController : UINavigationController <UINavigationBarDelegate>
【讨论】:
以上是关于UINavigationController 不适用于 SearchDisplay 控制器的主要内容,如果未能解决你的问题,请参考以下文章
UINavigationController 不适用于 SearchDisplay 控制器
UINavigationController 中的 UITabBarController 适用于 iOS 8 但不适用于 7
Swift 3 UIStatusBarStyle 不适用于 UINavigationController 中的单独 ViewController
UINavigationBar backgroundColor 不适用于 UITabBar
segue 不适用于 UITableViewCell alloc,但 dequeueReusableCellWithIdentifier