Skip to content

Instantly share code, notes, and snippets.

@tjhv
Created February 17, 2018 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjhv/25b7f85307eec6c1dc25282133ebf395 to your computer and use it in GitHub Desktop.
Save tjhv/25b7f85307eec6c1dc25282133ebf395 to your computer and use it in GitHub Desktop.
View controller loader
//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