UINavigationController子类 - 自定义Pop

时间:2012-04-11 15:51:27

标签: objective-c ios uinavigationcontroller interface-builder subclass

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

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

但我无法得到一个简单的测试用例。这是我试过的......

我创建了一个新类CustomNavigationController,它继承自UINavigationController。在这里,我更新了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

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

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

2 个答案:

答案 0 :(得分:1)

来自UINavigationController文档的第一段。

  

UINavigationController类实现了一个专门的视图   管理分层内容导航的控制器。这个   class不用于子类化。相反,您使用的实例   在你想要你的应用程序的用户的情况下它   界面,以反映您的内容的层次性。

如果您想要这种自定义,最好实现自己的导航样式控制器。

答案 1 :(得分:1)

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

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

enter image description here