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]
.
Written by Adam Yanalunas
Related protips
3 Responses

+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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#