Question

I have a procedure that I'll need in a lot (if not all) of my view controllers. I want to know how I can put it in one place (for code cleanliness and maintenance) and utilize it elsewhere.

Was it helpful?

Solution

There are more ways on how to approach this - depending on what exactly you would like to achieve.

If this methods are tied with UIViewController's life and data you would probably want to subclass UIViewController or make an UIViewController category.

A: Subclassing (you want to add some custom properties, variables, methods or you want to override a method):

MySubclassedViewController.h

#import <UIKit/UIKit.h>

@interface MySubclassedViewController : UIViewController

@property (copy) NSString *myVerySpecialString;

-(void) myVerySpecialMethod;

@end

MySubclassedViewController.m

#import "MySubclassedViewController.h"

@implementation MySubclassedViewController

-(void) initialization
{
    self.myVerySpecialString = @"initialized";

}

- (id)initWithCoder:(NSCoder*)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self)
    {
        [self initialization];
    }

    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        [self initialization];
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        [self initialization];
    }
    return self;
}

-(void) viewDidLoad
{
    [super viewDidLoad];

    [self myVerySpecialMethod];
}

-(void) myVerySpecialMethod
{
    if ([self.myVerySpecialString isEqualToString: @"initialized"])
    {
        self.backgroundColor = [UIColor yellowColor];
    }
}

@end

B: Category (you just want to add some extra method to a class):

UIViewController+SpecialCatrgory.h

#import <UIKit/UIKit.h>

@interface UIViewController (SpecialCategory)

-(void) myVerySpecialMethod;

@end

UIViewController+SpecialCatrgory.m

#import "UIViewController+SpecialCatrgory.h"

@implementation UIViewController (SpecialCategory)

-(void) myVerySpecialMethod
{
    self.backgroundColor = [UIColor yellowColor];   
}

@end

C: Dedicated helper class

On the other hand if you find yourself using some independent method on more than one place you might want to consider writing up a helper class and use it as a singleton.

MyHelperClass.h

@interface MyHelperClass : NSObject

+ (instancetype)sharedHelper;

-(NSString *) myVerySpecialMethod;

@end

MyHelperClass.m

#import "MyHelperClass.h"

@implementation MyHelperClass

+ (instancetype)sharedHelper
{
    static MyHelperClass *_sharedHelper = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        _sharedHelper = [[MAConnectionClient alloc] init];
    });

    return _sharedHelper;
}

-(NSString *) myVerySpecialMethod
{
    return @"a result from your very special method";
}

@end

You use it simply by importing MyHelperClass.h (or putting it in -Prefix.pch), without explicitly creating an instance. For example:

NSString *someString = [[MyHelperClass sharedHelper] myVerySpecialMethod];

OTHER TIPS

There are many ways to achieve this.

  1. Create a base viewcontroller and add your procedure. Subclass this in all your view controller.

  2. Create a common utility class and add your procedure and make it as a class method.

  3. Add your procedure in .pch file.

We have many ways to do it but in general create one global class, import it in YourProjectName-Prefix.pch file.


We can also go for another way i.e create any class method and you can call it anywhere through it's Class Name.

One example, you might have seen many times in your code- :

In appDelegate.h file, if we make this method and implement it in appDelegate.m file then

+ (NSString *)applicationDocumentDir
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

then we can access it from anywhere in the code like :

NSString *strTemp = [AppDelegate applicationDocumentDir];

To know more better way have a look here.

Use of a singleton for creating a basic style helper class

If you're not sure that the method will be used in all Controllers, I'd recommend creating a category for the functionality you're adding. You can find a good intro to categories here.

If you're sure you'll need it in all UIViewControllers, creating a base Controller and subclassing should be the better approach.

All other methods of implementing (Placing a class method in a utility / Adding to *-Prefix.pch) will work, but might not be ideal solutions (assuming the functionality your're adding is only applicable to UIViewController).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top