Objective C - 从 nib 加载 UIView?
我有一个基类,它从 nib 文件初始化自身。 我如何继承这个类
。 每次我初始化它的子类
时,它都会创建基类的对象而不是实际的类
我正在尝试创建
基类
@implementation BaseClass
- (id)init{
self = [[[[NSBundle mainBundle] loadNibNamed:@"BaseClass"
owner:self
options:nil] lastObject] retain];
if (self){
}
return self;
}
@end
A类
@implementation MyClass //Inheriting from BaseClass
- (void)init {
self = [super init];
if (self) {
}
return self;
}
- (void)methodSpecificToThisClass {
//do something
}
@end
用法
// It crashes when I call 'methodSpecificToThisClass'
// because the type that has been created is a type
// of my BaseClass instead of MyClass
MyClass *myClass = [[MyClass alloc] init];
[myClass methodSpecificToThisClass];
I have a base class that initializes itself from a nib
file.
How can I inherit from this class
.
Every time I init a subclass
of it it creates an object of the base class instead of the actual class
I am trying to create
Base Class
@implementation BaseClass
- (id)init{
self = [[[[NSBundle mainBundle] loadNibNamed:@"BaseClass"
owner:self
options:nil] lastObject] retain];
if (self){
}
return self;
}
@end
A Class
@implementation MyClass //Inheriting from BaseClass
- (void)init {
self = [super init];
if (self) {
}
return self;
}
- (void)methodSpecificToThisClass {
//do something
}
@end
Usage
// It crashes when I call 'methodSpecificToThisClass'
// because the type that has been created is a type
// of my BaseClass instead of MyClass
MyClass *myClass = [[MyClass alloc] init];
[myClass methodSpecificToThisClass];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 self = [[[[NSBundle mainBundle] loadNibNamed:@"BaseClass"owner:self options:nil] lastObject]retain]; 更改为
self = [[[[NSBundle mainBundle] loadNibNamed: NSStringFromClass([self class])owner:self options:nil]lastObject]retain];
这当然假设您对每种视图类型都有单独的笔尖。
Change
self = [[[[NSBundle mainBundle] loadNibNamed:@"BaseClass" owner:self options:nil] lastObject] retain];
toself = [[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject] retain];
This is of course assuming you have separate nibs foe each view type.
因为您总是从同一个 nib 文件加载对象,所以您总是获得相同的对象。如果笔尖中的对象属于
BaseClass
类型,那么当您加载笔尖时,您将获得一个BaseClass
类型的对象。您分配一个MyClass
并不重要——您分配的东西不会发挥作用,因为您将self
分配给加载的对象。 (事实上,由于您在重新分配 self 之前从未释放过分配的内存,因此您正在泄漏分配的内存。)将指针声明为MyClass*
- 让您可以调用-methodSpecificToThisClass
而不会收到编译器的投诉,但它不会改变实际对象是实例的事实>基类
。当您调用该方法时,当运行时尝试解析选择器并发现该对象不存在它时,您会收到“未实现的选择器”错误。如果您想从笔尖加载
MyClass
,则必须使用包含MyClass
而不是BaseClass
的笔尖。Because you're always loading objects from the same nib file, you always get the same objects. If the object in the nib is of type
BaseClass
, then you're going to get an object of typeBaseClass
when you load the nib. It doesn't matter that you're alloc'ing aMyClass
-- the thing that you alloc doesn't come into play because you assignself
to the loaded object. (In fact, since you never release the alloc'ed memory before reassigningself
, you're leaking the alloc'ed memory.) It also doesn't matter that you've declared the pointer as aMyClass*
-- that lets you call-methodSpecificToThisClass
without getting a complaint from the compiler, but it doesn't change the fact that the actual object is an instance ofBaseClass
. When you do call that method, you get an "unimplemented selector" error when the runtime tries to resolve the selector and finds that it doesn't exist for that object.If you want to load a
MyClass
from a nib, you're going to have to use a nib that contains aMyClass
instead of aBaseClass
.