Loading UIViewController "from" Nib File

JayVDiyk picture JayVDiyk · May 19, 2015 · Viewed 35.1k times · Source

I am trying to create an UIViewController from a Nib file. On Google I found that I can only load an UIView from Nib.

But some suggests that I could create a Nib file (of UIView) whose File Owner is set to our ViewController.

That is what I did, I got no crashes, but the View is just not displayed.

EDIT: Tried to push the viewcontroller like this

self.navigationController!.pushViewController(CoolViewController(), animated: true );

But it still showing black screen when it is pushed

XCode 6.3 - Not using Storyboards

Answer

Sudhin Davis picture Sudhin Davis · May 19, 2015

Try this, you can load a UIViewController with nibName:

Swift

self.navigationController!.pushViewController(CoolViewController(nibName: "CoolViewControllerNibName", bundle: nil), animated: true )

Objective-C

CoolViewController*coolViewCtrlObj=[[CoolViewController alloc] initWithNibName:@"CoolViewControllerNibName" bundle:nil];
[self.navigationController pushViewController:coolViewCtrlObj  animated:YES];