Search code examples
objective-cmacoscocoansbundle

Trying to replace deprecated loadnibnamed:owner


I'm trying to replace the deprecated

[NSBundle loadNibNamed:@"Subscriptions" owner:self];

with this instead (only thing I can find that's equivalent)

[[NSBundle mainBundle] loadNibNamed:@"Subscriptions" owner:self topLevelObjects:nil];

but the dialog pops up and disappears right away instead of staying open like it was doing with the deprecated call.

This code is inside a viewcontroller like this.

- (id)init{
    self = [super init];
    if (self) {
        //[NSBundle loadNibNamed:@"Subscriptions" owner:self];

        [[NSBundle mainBundle] loadNibNamed:@"Subscriptions" owner:self topLevelObjects:nil];
    }
    return self;

}

and I'm calling it from the appdelegate like this.

SubscriptionsViewController *subscriptionsViewController = [[SubscriptionsViewController alloc] init];
[subscriptionsViewController.window makeKeyAndOrderFront:self];

Is there anything I'm missing? It seems straight forward to me.


Solution

  • The dialog appearing and then disappearing is a sign of possible object collection - with a strong reference to the dialog it will be collected and lost.

    The deprecated call retained ownership of the top-level objects in the nib, the new call does not.

    Therefore the properties of the owner object that refer to top-level objects must be strong, or you need to keep the top-level objects array. This is contrary to the old recommendation where such properties were weak.

    Properties which reference non-top-level objects in the nib can still be weak.