iPhone - 从笔尖加载带有自定义 UI 的 UIView?

发布于 2024-11-24 18:24:17 字数 658 浏览 3 评论 0原文

我有一个 UIView,我从 nib 文件初始化它。

在我的 nib 文件中,我拖放了一个 UIImageView 并将类名更改为 MyImage。

当我加载视图时,它似乎没有使用我的自定义类初始化图像,因为 init 方法没有被调用。 知道问题是什么吗?

@interface MyView : UIView
@property (nonatomic, retain) IBOutlet MyImage *image;
@end

@implementation MyView
@synthesize image;

- (id)init
{
   self = [super initWithNibName:@"MyView" bundle:nibBundleOrNil];
   return self;
}
@end

这是我的图片 这是我的图片

@interface MyImage : UIImageView
@end

@implementation MyImage
- (id)init
{
   // This doesn't get called 
   self = [super init];
   if (self) 
   {
      // do somethin
   }
   return self;
}
@end

I have a UIView and I initialize it from a nib file.

In my nib file I dragged and dropped a UIImageView and I changed the class name to MyImage.

When i load the view it doesn't seem like it's initializing the the image using my custom class, because the init method is not getting called.
Any idea what the problem is?

@interface MyView : UIView
@property (nonatomic, retain) IBOutlet MyImage *image;
@end

@implementation MyView
@synthesize image;

- (id)init
{
   self = [super initWithNibName:@"MyView" bundle:nibBundleOrNil];
   return self;
}
@end

Here is MyImage
Here is my Image

@interface MyImage : UIImageView
@end

@implementation MyImage
- (id)init
{
   // This doesn't get called 
   self = [super init];
   if (self) 
   {
      // do somethin
   }
   return self;
}
@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

[浮城] 2024-12-01 18:24:17

从笔尖加载视图时使用的初始值设定项是 -initWithCoder:,而不是 -init。从 UIView 参考页面

initWithCoder:—如果您从某个位置加载视图,请实现此方法
Interface Builder nib 文件和您的视图需要自定义
初始化。

此外,如果您以编程方式实例化视图,则通常的初始化程序是 -initWithFrame:

因此,将您的 -init 方法更改为 -initWithCoder:,或实现 -initWithCoder: 以便它调用您的 -init.

The initializer that's used when loading a view from a nib is -initWithCoder:, not -init. From the UIView reference page:

initWithCoder:—Implement this method if you load your view from an
Interface Builder nib file and your view requires custom
initialization.

Moreover, if you're instantiating the view programmatically, the usual initializer is -initWithFrame:.

So, change your -init method to -initWithCoder:, or implement -initWithCoder: such that it calls your -init.

不羁少年 2024-12-01 18:24:17

Caleb 是对的,像这样实现它:

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        self.userInteractionEnabled = YES;
        // other stuff
    }
    return self;
}

Caleb is right, implement it like so :

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        self.userInteractionEnabled = YES;
        // other stuff
    }
    return self;
}
星星的轨迹 2024-12-01 18:24:17

我认为 awakeFromNib 更安全,因为所有插座都正确连接

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        //self.userInteractionEnabled = YES;
        // other stuff
    }
    return self;
}

- (void)awakeFromNib
{
    ..setup view
}

按顺序调用:

initWithCoder
awakeFromNib

I though awakeFromNib was safer as all outlets wired up correctly

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        //self.userInteractionEnabled = YES;
        // other stuff
    }
    return self;
}

- (void)awakeFromNib
{
    ..setup view
}

Called in order:

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