简体   繁体   English

我如何知道是否已加载NIB文件?

[英]How do I know whether a NIB file is loaded or not?

I made a series of UIView subclasses. 我做了一系列的UIView子类。 Each is inherited from another. 每个都是从另一个继承的。

In the grand grand parents of all the subclass is this code: 在所有子类的祖父代中,此代码是:

NSString * className = NSStringFromClass([self class]);
[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil];

The problem with this code is that not all subclasses have their own NIB. 此代码的问题在于,并非所有子类都有自己的NIB。 Some resort to using the NIB of their parent class. 有些人求助于使用其父类的NIB。

I want to check whether the UIView has been loaded from NIB or not before executing the code. 我想在执行代码之前检查是否已经从NIB加载了UIView

How would I do so? 我该怎么办?

That code will not works as you expected, sorry: you use NSStringFromClass([self class]) to get the nib name, so if it doesn't exist a nib with the class name it will not loaded and the object will be nil. 抱歉,该代码将无法正常工作:您使用NSStringFromClass([self class])获取笔尖名称,因此,如果不存在带有类名的笔尖,则不会加载该笔尖,并且该对象将为nil。 There's no sort of inheritance in NIB. NIB没有任何继承。 You could try using explicit nib name when the classname fails. 当类名失败时,您可以尝试使用显式笔尖名称。 Something like: 就像是:

id myView = [[NSBundle mainBundle] loadNibNamed:className owner:self options:nil];
if (!myView)
    myView = [[NSBundle mainBundle] loadNibNamed:@"defaultNIBname" owner:self options:nil];

or you could get all the object hierarchy classes and try them all till you find one NIB.. 或者您可以获取所有对象层次结构类,并尝试所有这些类,直到找到一个NIB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM