ViewController exit view first non-nil, then nil when loading from the bundle

I am loading a UIViewController from a package with initWithNibName:bundle: If I set a breakpoint in viewDidLoad , I see that its view . I also see this when viewing About.xib in the interface builder.

However, as soon as the view is used for the first time (when I call [self.navigationController pushViewController:viewController animated:YES] according to my application logic), I get this error:

* Application termination due to the uncaught exception "NSInternalInconsistencyException", reason: '- [UIViewController _loadViewFromNibNamed: bundle:] loaded "O", but the exit viewpoint was not set. ''

This is a call that also calls viewDidLoad . Thus, it is obvious that during the call, view is the first non-nil (as it should be), and later, obviously, becomes nil again.

If I alternatively type po [viewController view] in the debugger just before the call, I get this error (which is supposedly just a different representation of the same symptom):

Error: execution aborted, reason: ObjC internal exception breakpoint (-3). The process was returned to the state before evaluating the expression.

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

UPDATE The problem seems to disappear if I load the view controller from my storyboard (not necessarily my main storyboard), and not from the XIB file.

+6
source share
2 answers

When viewing an xib file in Xcode, the identity inspector uses a custom subclass to view the controller class? If you set this, then go to the connection inspector, you will see the “view” attribute, just drag it into your view in the interface builder and it should work

+3
source

I assume that you have overridden the default behavior of the UIViewController , either by UIViewController its view a weak property, the weird loadView method loadView or simply not loadView super in one of your overridden methods.


Another common mistake is to call a non- super viewDidLoad super method (calling super viewDidLoad from awakeFromNib , etc.), which can happen when you move the code or try to do different things.

+3
source

Source: https://habr.com/ru/post/969992/


All Articles