Reputation: 35
I have a basic question about subclassing UIViewControllers
but can't seem to find the answer anywhere here. My app has a UINavigationViewController
and I plan to have several views (in sequence). My first ViewController
is a custom ViewController
with UITableView
, DatePicker
, and UIPickerController
view etc.
I would like to use the same ViewController
(I guess a subclass?) for all the subsequent views. I would like to use the same layout I build in storyboard. Currently my code is laid out such that in the viewDidLoad
, I read an array from plist and that populates the rest of the table and content.
As you can imagine, I am trying to avoid copy paste, duplicate code, and easy way to make changes to all the views simultaneously.
I know this should be rather trivial but I can't figure out what changes do I need to make. Also, should I create a ViewController
with xib? and is there is a way to drag my CustomViewController
into the list that appears at the right side in Xcode?
Upvotes: 0
Views: 1864
Reputation: 3567
In my opinion, you can create a viewController as a root viewController in which you can implement the common view, action and so on. And then, create other viewController subclass your rootViewController.
In your rootViewController, you can write some common codes, with or without xib. It's all dependents on you. Here is a simple example:
//Your RootViewController.
class rootViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button:UIButton = UIButton(frame: CGRectMake(10, 10, 200, 90))
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: "testButton:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(button)
}
func testButton(button:UIButton) {
// Some Common Code. or just keep blank.
println("parents")
}
}
// Your ChildrenViewController.
class childrenViewController: rootViewController {
override func testButton(button:UIButton) {
super.testButton(button)
// Some code only for this children view controller.
println("children")
}
}
Because childrenViewController is subclass of rootViewController, all rootViewController's function will be valid for it. So childrenViewController will display a redButton in frame(10, 10, 200, 90) automatically. And if you press the button ,this will print "parents" and "children".
hope it can help you.
Upvotes: 0
Reputation: 355
I think you should to create a 'base' view controller that will collect all repeating methods and properties. It will inherit from UIViewControll, but all other controllers will inherit from base.
Same views you can copy-paste in storyboard and connect with outlets in base class, but if you will need to change something you will have a problems.. So I recommend you to create it programmatically (I think xibs are already a past), or replace it into another small view controller and then use container of view controllers to load it.
If you have a some things that repeating but not in each controller, you can create your own options in base class like this:
typedef NS_OPTIONS(NSUInteger, BaseOptions) {
BaseOptionsNone = 0,
BaseOptionsTableView = 1 << 0,
BaseOptionsDatePicker = 1 << 1,
BaseOptionsSomethingElse = 1 << 2
};
@interface BaseViewController : UIViewController
@property (nonatomic) BaseOptions options;
@end
@implementation BaseViewController
- (void)setOptions:(BaseOptions)options {
_options = options;
if (self.options & BaseOptionsTableView) {
// add table view
}
if (self.options & BaseOptionsSomethingElse) {
// another staff
}
}
@end
And then set options into subclass:
@interface CustomViewController : BaseViewController
@end
@implementation CustomViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.options = BaseOptionsTableView | BaseOptionsSomethingElse;
}
@end
Have a fun :)
Upvotes: 1