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.
-
You could subclass UIViewController or you could make a UIViewController category – Rok Jarc Dec 21 '13 at 14:24
-
care to define what kind of "_procedure_" you need to reuse? – staticVoidMan Dec 21 '13 at 17:14
-
It's all ready been answered and I'm satisfied with the answer but thanks man. – SonOfSeuss Dec 21 '13 at 17:28
4 Answers
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];

- 18,765
- 9
- 69
- 124
-
Can you explain the idea behind the sharedHelper method in the MyHelperClass.m file? What is the reason for it? Can you explain it? It's a lot to ask but it'd be a big help. – SonOfSeuss Dec 21 '13 at 15:26
-
1It is created and used as a singleton. It might not be the best approach for your problem (you didn't provide much data on what kind of method you want to use). I typically use one such singleton as a DataModel [example](http://stackoverflow.com/a/16199974/653513) (object that hold all the data for my app). You might want to use one to hold some independent helper methods (methods that don't rely on any special class/object). It's beauty is that it creates it self when you first call it and it is available from any place of your code. – Rok Jarc Dec 21 '13 at 15:35
-
If the method you want to use is really something that has to do with UIVIewController then choose between solution A or B. C is mentioned more as an extra :) – Rok Jarc Dec 21 '13 at 15:36
-
There are many ways to achieve this.
Create a base viewcontroller and add your procedure. Subclass this in all your view controller.
Create a common utility class and add your procedure and make it as a class method.
Add your procedure in .pch file.

- 524
- 3
- 5
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.
-
1AppDelegate is a wrong place for helper methods. In that case it is better to make a dedicated helper class as an singleton and add it this helper methods. I'm not saying it doesn't work - it is just a bad practice (code readability and reusability). – Rok Jarc Dec 21 '13 at 14:30
-
2
-
1@rokjarc can you post an example of what your saying in your comment? – SonOfSeuss Dec 21 '13 at 14:42
-
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 UIViewController
s, 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
).

- 1,014
- 11
- 28