显然,解决方案的轮廓可以在这里找到: UINavigationController and UINavigationBarDelegate.ShouldPopItem() with MonoTouch
在我的解决方案,我只是有我所有的视图控制器扩展CustomUINavigationController,它看起来像这样:
#import "CustomUINavigationController.h"
#import "IOSVersion.h"
@interface CustomUINavigationController()
@end
@implementation CustomUINavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.delegate=self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//
// The following two methods are the key to overriding the buggy behavior in IOS 8
// The first method is from here:
// https://stackoverflow.com/questions/6413595/uinavigationcontroller-and-uinavigationbardelegate-shouldpopitem-with-monotouc
//
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
NSLog(@"Inside shouldPopItem");
if (regularPop) {
NSLog(@"regularPop is TRUE");
} else {
NSLog(@"regularPop is FALSE");
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
if (regularPop) {
regularPop = FALSE;
return YES;
}
regularPop = TRUE;
[self popViewControllerAnimated:YES];
return NO;
} else {
return [super navigationBar:navigationBar shouldPopItem:item];
}
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
NSLog(@"Inside popViewControllerAnimated");
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
int cnt=(int)[viewControllers count];
NSLog(@"Inside popViewControllerAnimated, cnt is %d",cnt);
UIViewController *vc=[viewControllers objectAtIndex:cnt-2];
if (regularPop) self.desiredVC=vc;
[self popToViewController:vc animated:animated];
return vc;
} else {
return [super popViewControllerAnimated:animated];
}
}
- (UIViewController *)manualpopViewControllerAnimated:(BOOL)animated {
NSLog(@"Inside manualpopViewControllerAnimated");
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
regularPop=TRUE;
return [self popViewControllerAnimated:animated];
} else {
return [super popViewControllerAnimated:animated];
}
}
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated {
NSLog(@"Inside didShowViewController");
if (viewController==self.desiredVC) {
NSLog(@"Inside didShowViewController, found desiredVC");
regularPop = FALSE;
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
你可能要包括哪些你用来推入导航堆栈的机制。你使用故事板吗?你在使用segues吗?你使用pushViewController:?另外,你的日志中是否提到了有关腐败导航堆栈的任何内容? – atreat 2014-10-16 20:39:38
我只是使用旧的标准pushViewController。我不使用storyboard或segues。这是非常基本的IOS编码,可以在旧版本的IOS和XCode中正常工作,但似乎在使用XCode 6的IOS8中被破坏。 – Marc 2014-10-16 21:09:15
您是否得到了解决方案?我有同样的问题 – 2014-11-26 09:02:33