简体   繁体   中英

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

or

2) written once in a block of code but allocated many times as needed by each view.

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

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). 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 . 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.

@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.

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. This will make your methods available on every UIViewController, including all the standard subclasses, without any extra work on your part. With this solution you cannot directly add ivars, although you can fake it up well enough using associative references .

Well, what I ended up doing was to create a Singleton object and put all my common methods there. This meant that all my View Controllers could utilize the common methods of the 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.

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. I do thank the 3 who gave the time to answer.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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