Trying to replace deprecated loadnibnamed:owner

六月ゝ 毕业季﹏ 提交于 2019-12-23 10:19:12

问题


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.


回答1:


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.




回答2:


I just had a similar problem when using loadNibNamed: owner: topLevelObjects: and always got an error like

[__NSArrayM insertObject:atIndex:]: object cannot be nil' terminating with uncaught exception of type NSException abort() called

Because my top level objects where nil.

I finally discovered that the nib file I was loading had its Interface Builder version set to "Xcode 4.6". When I set that to 6.2, everything worked fine again.



来源:https://stackoverflow.com/questions/19602390/trying-to-replace-deprecated-loadnibnamedowner

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