I recently asked a question regarding my incorrect use of instance variables on UIViewControllers
. The answers I got brought to light another fundamental issue: I think I might be using UIViewControllers
all wrong as well.
My typical storyboard looks something like this:
As you can see, each view controller has a custom class. One of those custom classes might look like this:
// SecondViewController.h
#import <UIKit/UIKit.h>
#import "MasterViewController.h"
@interface SecondViewController : MasterViewController
@property (strong, nonatomic) NSData *incomingData;
@end
// SecondViewController.m
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// do stuff with data, then pass it on to next view controller.
}
@end
Each view controller is a subclass of MasterViewController
. In MasterViewController
, I'll usually put global things like user state validation, for example. That way every view controller in the app can validate user state without reimplementing it each time.
About that incomingData
property...I wasn't sure how else to pass data between UIViewControllers
without using a property on a custom class...hence three separate custom classes, each one not doing all that much.
So here's where it gets murky for me...
- Should I be keeping important methods and instance variables in a "master" view controller so all other view controllers can subclass it and have access to those methods and variables? Or is that ridiculous?
- Am I causing more work for myself by not using proper class extensions or categories? Would those help me keep things a little more streamlined and organized?
My current system works for the most part, but it can get unwieldy when I have lots and lots of view controllers, each with its own custom class.