Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewController's outlet view first non-nil, then nil when loading from bundle

I am loading a UIViewController from a bundle with initWithNibName:bundle:. If I set a breakpoint in its viewDidLoad I can see that its view is set. I can also see this when viewing About.xib in the Interface Builder.

However once the view is actually used for the first time (in a call to [self.navigationController pushViewController:viewController animated:YES] according to my app's logic) I get this error:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "About" nib but the view outlet was not set.'

This is the call that also triggers viewDidLoad. So apparently during the call, view is first non-nil (as it should be) and later apparently becomes nil again.

If I alternatively type po [viewController view] in the debugger immediately prior to the call I get this error (which is presumably just another presentation of the same symptom):

error: Execution was interrupted, reason: internal ObjC exception breakpoint(-3).. The process has been returned to the state before expression evaluation.

How can I load and use the view controller in conjunction with an existing navigation bar without running into these errors?

UPDATE The problem apparently disappears if I load the view controller from my a storyboard (not necessarily my main storyboard) instead of from a XIB file.

like image 975
Drux Avatar asked May 28 '14 16:05

Drux


2 Answers

When viewing the xib file in Xcode, in the identity inspector, is the custom subclass set to you view controller's class? If you set that, then go to the connections inspector, you will see an attribute called "view", just drag that to your view in Interface builder and it should work

like image 184
Clint Warner Avatar answered Sep 25 '22 08:09

Clint Warner


My guess is that you were overriding some default UIViewController behavior, either make its view a weak property, a strange loadView method or just not calling super in one of your overridden methods.


Another common error is calling a non-matching super method (call super viewDidLoad from an awakeFromNib, etc.) which could happen when you move code around or after trying different things.

like image 33
Rivera Avatar answered Sep 21 '22 08:09

Rivera