简体   繁体   English

自定义类的Objective-C类别调用方法

[英]Objective-C category calling method of a custom class

I have some classes, lets call them A, B and C, being inherited from UIViewController and some category of UIViewController that has some methods in it that have to be common for all the classes. 我有一些类,让它们称为A,B和C,它们是从UIViewController继承而来的,并且其中的UIViewController类别中包含某些方法,这些方法对于所有类都是通用的。
Category's methods have to call another methods on ViewControllers, but. 类别的方法必须在ViewControllers上调用另一个方法。 Not all methods will be implemented in VCs (because they never be called in that context). 并非所有方法都将在VC中实现(因为在那种情况下永远不会调用它们)。
If I write method definition in category, I get an error, that method is not declared, but it is declared in VC or not declared and will never be used in that context. 如果我在类别中编写方法定义,则会收到错误消息,该方法未声明,但是在VC中声明或未声明,并且永远不会在该上下文中使用。
Is there any ideas how to override this and share huge part of code (Navigation code) between multiple ViewControllers? 有什么想法可以重写此方法,并在多个ViewController之间共享大部分代码(导航代码)吗?

- (void)homeButtonPressed {
    NSLog(@"Home button pressed, ViewController");
}

- (void)changeFloorButtonPressed {
    [self changeFloor];
    NSLog(@"Change Floor button pressed, ViewController");
}
- (void)QRButtonPressed {
    NSLog(@"QR button pressed, ViewController");
}  

Here [self changeFloor]; 这里[self changeFloor]; is method, which is specific only to one ViewController. 是方法,仅特定于一个ViewController。

Create an objective-c protocol which declares an @optional method for -(void)changeFloor; 创建一个Objective-C协议,该协议为-(void)changeFloor;声明一个@optional方法-(void)changeFloor; Then, in your category, conform to that protocol. 然后,在您的类别中,遵循该协议。 You will be able to call the changeFloor method without getting a compile error. 您将能够调用changeFloor方法而不会出现编译错误。

Because the method is optional, you must check if: 因为该方法是可选的,所以您必须检查:

[self respondsToSelector:@selector(changeFloor)] before calling changeFloor. 在调用changeFloor之前, [self respondsToSelector:@selector(changeFloor)]

edit: there is probably a way to architect your code so that you don't need to do this. 编辑:可能有一种方法来构造您的代码,以便您不需要这样做。

Why note create your base class of type UIViewController, then create classes that inherit from your new base class. 为什么要注意创建UIViewController类型的基类,然后创建从新基类继承的类。 Each child can then implement the methods they need. 然后每个孩子都可以实现他们所需的方法。

MyBaseViewController: UIViewController MyBaseViewController:UIViewController

ViewAViewController: MyBaseViewController ViewAViewController:MyBaseViewController

ViewBViewController: MyBaseViewController ViewBViewController:MyBaseViewController

etc 等等

I use this type of approach for universal apps, my child class can override the implementation from the parent or add to it. 我对通用应用程序使用了这种类型的方法,我的子类可以覆盖父类的实现或将其添加。

// common elements HomeViewController: UIViewController //常用元素HomeViewController:UIViewController

// iPhone specific implementation HomeViewController_iPhone : HomeViewController // iPhone特定的实现HomeViewController_iPhone:HomeViewController

// iPhone specific implementation HomeViewController_iPad: HomeViewController // iPhone特定的实现HomeViewController_iPad:HomeViewController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM