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

假装没事ソ 提交于 2019-12-08 19:59:06

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/23917607/viewcontrollers-outlet-view-first-non-nil-then-nil-when-loading-from-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!