简体   繁体   English

如何在Objective-C中的视图控制器之间共享公共方法?

[英]How do I share common methods between view controllers in Objective-C?

In my app there are numerous view controllers which have their own purposes. 在我的应用程序中有许多视图控制器,它们有自己的用途。 However, underneath they do share some need for common maintenance work that could use the same method code instead of each having its own copy of the code, literally pasted in. 但是,在它们下面确实需要共同的维护工作,这些工作可以使用相同的方法代码,而不是每个都有自己的代码副本,字面上粘贴。

Is there a way to share the code of these common methods ? 有没有办法分享这些常用方法的代码? I see two possibilities: 我看到两种可能性:

1) either one copy of code truly shared in memory as in a special maintenance object of methods 1)在方法的特殊维护对象中,在内存中真正共享的一个代码副本

or 要么

2) written once in a block of code but allocated many times as needed by each view. 2)在代码块中写入一次,但每个视图根据需要分配多次。

Which of these is the correct path or what is the correct path, and HOW would it be implemented in the most simple manner ? 以下哪一条是正确的路径或正确的路径,以及如何以最简单的方式实施?

Kindness pls, new coder in the room. 善良,在房间里的新编码器。

Thanks. 谢谢。

-Ric -Ric

Note: This is an old question/answer reflective of Apple practices at the time, and answered for a new coder looking for a simple solution they can understand (as requested in the question). 注意:这是一个反映当时Apple实践的旧问题/答案,并回答了一个新的编码人员正在寻找他们能够理解的简单解决方案(根据问题的要求)。 There are better and more testable ways to achieve this, using composition . 使用合成 ,有更好,更可测试的方法来实现这一目标。

The best way to achieve what you want is to create a common parent class for your view controllers. 实现所需目标的最佳方法是为视图控制器创建公共父类。 So instead of inheriting directly from UIViewController , each of your custom classes will inherit from SomeFeatureViewController (where SomeFeature describes the common feature provided), this class inherits from UIViewController . 因此,不是直接从UIViewController继承,而是每个自定义类都将继承自SomeFeatureViewControllerSomeFeature描述所提供的常用功能),此类继承自UIViewController Now, each of your actual view controllers will inherit from SomeFeatureViewController and any common methods (also any common instance variables used by these methods) can be placed in this parent class. 现在,每个实际的视图控制器都将继承自SomeFeatureViewController并且任何常用方法(也可以是这些方法使用的任何常见实例变量)都可以放在此父类中。

@interface SomeFeatureViewController : UIViewController
{
    int common_iVars;
}
- (void)commonMethods;
@end

@interface ActualViewController : SomeFeatureViewController
{
    int specific_iVars;
}
- (void)specificMethods;
@end

As well stated by jhabbott, a subclass of UIViewController is one good solution. 正如jhabbott所说,UIViewController的子类是一个很好的解决方案。

If that's not an option (eg you can't change the class hierarchy), another option is to create a category on UIViewController to add the methods and properties you need. 如果这不是一个选项(例如,你不能改变类层次结构),另一个选择是在UIViewController上创建一个类别来添加你需要的方法和属性。 This will make your methods available on every UIViewController, including all the standard subclasses, without any extra work on your part. 这将使您的方法在每个UIViewController上都可用,包括所有标准子类,而无需您做任何额外的工作。 With this solution you cannot directly add ivars, although you can fake it up well enough using associative references . 使用此解决方案,您无法直接添加ivars,尽管您可以使用关联引用将其伪装得足够好。

Well, what I ended up doing was to create a Singleton object and put all my common methods there. 好吧,我最终做的是创建一个Singleton对象并将所有常用方法放在那里。 This meant that all my View Controllers could utilize the common methods of the Singleton. 这意味着我的所有View控制器都可以使用Singleton的常用方法。 Maybe this is a bit too open if the project was being developed by a team of programmers but as it is just me I know that all the controllers have similar requirements in processing. 如果项目是由程序员团队开发的话,这可能有点过于开放,但正如我所知,所有控制器在处理方面都有类似的要求。 So the Singleton approach seemed to work. 所以Singleton方法似乎有效。

It also fit the project because at the time of the question all the View Controllers had been created and to make them a subclass of a parent class seemed to be a retrofit, though I agree that if part of an initial design, it may be a better approach. 它也适合项目,因为在问题发生时所有的View控制器都已创建并使它们成为父类的子类似乎是一种改进,尽管我同意如果是初始设计的一部分,它可能是一个更好的方法。 I do thank the 3 who gave the time to answer. 我真的感谢有时间回答的3。

The way I see it you should do a class with all the common methods and then subclass it. 我看到它的方式你应该用所有常用方法做一个类,然后继承它。 Say you have MyCommonViewController : UIViewController and then for each different view controller do MySpecificVIewController : MyCommonViewController 假设你有MyCommonViewController:UIViewController然后为每个不同的视图控制器做MySpecificVIewController:MyCommonViewController

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

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