Last Updated: February 25, 2016
· adamyanalunas

Testing nibs (Kiwi, OCUnit, KIF, whatevs)

Sometimes you forget the steps Cocoa/CocoaTouch does for you when instantiating a (NS|UI)ViewController.

If you're doing some programmatic testing of your view controllers and need to know that something in the nib loaded correctly you need to do two things:

UIViewController *myVC = [myVC.alloc initWithNib:@"nibFileName" bundle:nil];
[myVC loadView];

That's it. Now your nib will be unpacked and all elements initialized into its .view. Carry on doing fun things like [myVC.inputField.text shouldBeNil].

3 Responses
Add your response

+1 The documentation begins with "You should never call this method directly."

over 1 year ago ·

instead of [myVC loadView]; use;

UIView *v = myVC.view

UIViewController.view is lazy loaded; as soon we access it, it will create the view.

over 1 year ago ·

@johnhatvani Nice tip. Thanks!

over 1 year ago ·