PopViewController奇怪的行为

编程入门 行业动态 更新时间:2024-10-08 13:33:27
本文介绍了PopViewController奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于一个奇怪的请求,我试图拒绝,但它不起作用,我不得不覆盖navigationBar的后退按钮。

Due to a weird request which I tried to turn down but it didn't work, I had to override the navigationBar's back Button.

我做了一个自定义UINavigationController子类并入侵了 - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item 方法。

I have made a custom UINavigationController subclass and hacked the - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item method.

这是我的代码:

@interface CustomUINavigationController () @end @implementation CustomUINavigationController #pragma mark - UINavigationBar delegate methods - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item { if ([[self.viewControllers lastObject] isKindOfClass:[ViewController1 class]]) { ViewController1 *vc1 = (ViewController1 *)[self.viewControllers lastObject]; [vc1 handleBackAction]; if (vc1.canPopVC == YES) { [self popViewControllerAnimated:YES]; return YES; } else { return NO; } } [self popViewControllerAnimated:YES]; return YES; } @end

一切正常,除非是我以编程方式弹出一个viewController。每次我想在弹出后执行推送时,应用程序都会崩溃。在上转动 NSZombie,发现当以编程方式弹出viewController时,其父viewController被释放。 此时,制作自定义backButton不是一个选项,因为它会丢失原生iOS 7滑动到popViewController功能。

All works fine, except when I pop a viewController programmatically. The app crashed every time when I wanted to perform a push after said pop. Turning NSZombie on, revealed that when popping a viewController programmatically, its parent viewController is deallocated. At this point, making a custom backButton is not a option since it will lose the native iOS 7 swipe to popViewController feature.

崩溃日志:

*** -[ContactsDetailViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x1806b790

推荐答案

我不是百分百肯定,但我认为你不应该真的在弹出视图该委托方法中的控制器。

I'm not 100% certain but I don't think you should actually be popping the view controller in that delegate method.

应该委托方法通常不会做某事。他们只断言是否应该做某事。

"should" delegate methods don't normally do something. They just assert whether something should or shouldn't be done.

将你的方法改为...

Change your method to this...

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item { if ([[self.viewControllers lastObject] isKindOfClass:[ViewController1 class]]) { ViewController1 *vc1 = (ViewController1 *)[self.viewControllers lastObject]; [vc1 handleBackAction]; if (vc1.canPopVC == YES) { return YES; } else { return NO; } } return YES; }

看看它是否有效。

我所做的就是删除 popViewController 来电。

All I have done is removed the popViewController calls.

编辑 - 如何添加自定义后退按钮

在上的类别中UIBarButtonItem ...

+ (UIBarButtonItem *)customBackButtonWithTarget:(id)target action:(@SEL)action { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:@"Some image"] forState:UIControlStateNormal]; [button setTitle:@"Some Title" forState:UIControlStateNormal]; [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button]; return barButtonItem; }

现在,只要您想设置自定义后退按钮,只需使用...

Now whenever you want to set a custom back button just use...

UIBarButtonItem *backButton = [UIBarButtonItem customBackButtonWithTarget:self action:@selector(backButtonPressed)];

更多推荐

PopViewController奇怪的行为

本文发布于:2023-11-27 09:53:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   PopViewController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!