Objective C - 从 nib 加载 UIView?

发布于 2024-11-27 09:52:44 字数 923 浏览 0 评论 0原文

我有一个基类,它从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念绕指尖 2024-12-04 09:52:44

将 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]; to self = [[[[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.

子栖 2024-12-04 09:52:44

因为您总是从同一个 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 type BaseClass when you load the nib. It doesn't matter that you're alloc'ing a MyClass -- the thing that you alloc doesn't come into play because you assign self to the loaded object. (In fact, since you never release the alloc'ed memory before reassigning self, you're leaking the alloc'ed memory.) It also doesn't matter that you've declared the pointer as a MyClass* -- 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 of BaseClass. 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 a MyClass instead of a BaseClass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文