UINavigationController子类 - 自定义Pop [英] UINavigationController subclass - customizing Pop

查看:103
本文介绍了UINavigationController子类 - 自定义Pop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与许多其他人一样,我想在UINavigationController中使用后退按钮时执行操作。我知道一些替代方法,例如添加我自己的按钮并尝试欺骗外观,使用viewWillDisappear并在所有非后退操作上设置标记,这将导致视图消失等等。

Like many others, I would like to perform an action when the 'back' button is used in a UINavigationController. I am aware of some alternative approaches to this, such as adding my own button and trying to spoof the look, using viewWillDisappear and setting tags on all non-back actions that will cause the view do disappear, etc.

但是我想尝试的是继承UINavigationController并在popViewControllerAnimated中注入一些内容:

But the one that I would like to try is subclassing UINavigationController and injecting something in popViewControllerAnimated:

但我不能简单测试用例工作。这是我试过的...

But I can't get a simple test case to work. Here is what I tried...

我创建了一个新类CustomNavigationController,它继承自UINavigationController。在这里,我更新了popViewControllerAnimated:

I created a new class, CustomNavigationController, which inherits from UINavigationController. In here, I updated popViewControllerAnimated:

.h:

#import <UIKit/UIKit.h>

@interface CustomNavigationController : UINavigationController

@end

.m:

#import "CustomNavigationController.h"

@interface CustomNavigationController ()

@end

@implementation CustomNavigationController

- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
    NSLog(@"pop!");
    return [super popViewControllerAnimated:animated];
}

@end

在界面构建器中,我更改了导航控制器的类:

In interface builder, I changed the class of the navigation controller:

我错过了什么?每当我在这个导航控制器中弹出任何视图时,我都希望能够记录'pop'。

What am I missing? I am expecting 'pop' to be logged whenever I pop any view within this nav controller.

推荐答案

我刚刚测试了在iPhone 5.1模拟器上的实现非常相似,它运行正常 - 似乎问题出在其他地方。

I've just tested the very same implementation on iPhone 5.1 simulator and it works fine - it seems the problem is somewhere else.

我从单视图应用程序模板创建了一个空项目添加了CustomNavigationController类,其中包含您在问题正文中发布的代码。 UIButton用于将另一个视图控制器推送到导航堆栈(使用storyboard的segue),并且按预期在控制台中发布POP消息。

I've created an empty project from a single-view application template and added CustomNavigationController class with the code which you posted in the question body. An UIButton is used to push another view controller to navigation stack (using storyboard's segue), and 'POP' message is being posted in the console as expected.

这篇关于UINavigationController子类 - 自定义Pop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆