在编写Objective-C类别时,我如何知道要覆盖哪些方法? [英] How Do I Know Which Methods to Override When Writing an Objective-C Category?

查看:122
本文介绍了在编写Objective-C类别时,我如何知道要覆盖哪些方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UINavigationItem上写一个类别,以在整个应用程序中对barBackButtonItem进行更改.

I want to write a category on UINavigationItem to make changes to barBackButtonItem across my entire app.

根据我在此处的评论(更改所有UIViewController的BackBarButtonItem吗?),我应该重写backBarButtonItem,那么每当需要调用其后退按钮按钮项时,就会调用您的方法." -但是我如何知道要覆盖的方法?我看过UINavigationItem文档,并且有多种方法用于初始化backBarButtonItem.如何确定我应该在类别中覆盖的方法?

From what I have been told in the comments here ( Change BackBarButtonItem for All UIViewControllers? ), I should "override backBarButtonItem in it, then your method will be called whenever their back bar button item is called for." - but how do I know what method to override? I have looked at the UINavigationItem documentation, and there are multiple methods used for initializing a backBarButtonItem. How do I determine which method I should override in my category?

推荐答案

您希望使用UIViewController子类,而不是类别.

You want a subclass of UIViewController instead of a catagory.

例如:

@interface CustomViewController : UIViewController

@end

@implementation CustomViewController

-(void) viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.backBarButtonItem.title = @"";
}

@end

现在,您只需要为视图控制器使用CustomViewController类,它们都将应用更改.

Now you just need to use the CustomViewController class for your view controllers, and they will all have the changes applied to them.

如果您以编程方式执行此操作,则只想更改视图控制器的超类:

If you're doing this programatically, then you'll just want to change the superclass of the view controllers:

从此...到此...

如果使用情节提要,则需要在Identity Inspector中更改超类...

If you're using storyboards, you'll want to change the superclass from within the Identity Inspector...

这篇关于在编写Objective-C类别时,我如何知道要覆盖哪些方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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