Created
February 17, 2018 05:20
-
-
Save tjhv/25b7f85307eec6c1dc25282133ebf395 to your computer and use it in GitHub Desktop.
View controller loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//convenient method when working with nibs.. | |
//should be used within a view controller subclass | |
//name your nib the same as your view controller class name | |
//this will load it given it's in the main bundle, and | |
//it will append a child to the view in context (inView:) | |
//if view is nil, obviously it will not add a subview | |
//but the view controller will still be added as a child. | |
//did not bother to implement as a UIViewController category, | |
//minor error testing but worked without issue for my intended use. | |
//newly created view controllers view frame gets changed to the view | |
//in context, in case of use with container view controllers | |
- (void)loadViewController:(Class)class | |
inView:(UIView *)view | |
{ | |
if([class isSubclassOfClass:UIViewController.class]) | |
{ | |
UIViewController *viewController = ({ | |
NSString *className = NSStringFromClass(class); | |
[[class alloc] initWithNibName:className | |
bundle:nil]; | |
}); | |
[self addChildViewController:viewController]; | |
[viewController didMoveToParentViewController:self]; | |
viewController.view.frame = view.frame; | |
[view addSubview:viewController.view]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment