iphone dev:UIImageview 子类接口生成器 - 如何调用自定义初始值设定项

发布于 2024-08-24 08:47:39 字数 723 浏览 3 评论 0原文

我搞乱了iphone的开发。我有一个 uiimageview 子类,我想用它来检测触摸。我已成功添加到 InterfaceBuilder,并且可以在应用程序中检测到 UIImageview 子类中的触摸,因此在这方面一切都很好。但是我的 UIImageView 子类有一个自定义初始值设定项,在界面生成器中创建它时不会调用它。

如果我手动初始化 UIImageview 并以编程方式添加它,我认为它会起作用,但随后我就失去了在界面生成器中“查看”我的位置的能力。

我怎样才能

1) 在代码中添加的界面生成器中“查看”uiimageview? (不可能?) 2)当子类在interfacebuilder中实例化时调用我的自定义初始化程序。

谢谢


你好,谢谢你的建议。我想我越来越了解 xib 和 viewcontroller 之间的关系了。

我现在成功地以编程方式添加了 UIImageView 子类,并使用覆盖 InitWithFrame 的自定义初始化程序。

我想我读到 xib 调用“awakeFromNib”,所以我可以同样在其中添加我的初始化代码。我喜欢以编程方式添加它的想法,因为我有更多的控制权(尽管更难设置我的 IU),

所以还有一个相关问题。如果我在界面生成器中添加 UIImageView 子类。我可以看到它并检测到对其的触摸。如果我想在视图控制器类中引用它,我是否有指向它的指针?即它有一个变量名吗?我自己创建的 UIImageViews 我显然知道它们叫什么......

I messing with iphone developement. I have a uiimageview subclass that I want to use to detect touches. I have sucessfully added to interfacebuilder and I can detect a touch in my UIImageview subclass within my application so all is good on that front. however my UIImageView subclass has a custom initializer which is not called when it is created in interface builder.

if I manually initialize the UIImageview and add it programmatically I think it will work but then I lose the ability to 'see' my positioning in Interface builder.

how can I either

1) 'see' a uiimageview in interface builder that is added in code? (not possible?)
2) call my custom initializer when the subclass is instantiated in interfacebuilder.

thanks


Hi thanks for suggestions. I think I'm getting closer to understanding the relationship between the xib and the viewcontroller.

I now am sucessfully adding my UIImageView subclass programmatically and using my custom initiializer which overrides InitWithFrame.

I think I read that the xib calls 'awakeFromNib' so I could equally add my iniitialization code in there. I like the idea of adding it programmatically as I have more control (although harderto set up my IU)

so one more realted question. if I add an UIImageView subclass in interface builder. I can see it and detect touches on it. if I want to refer to it in the view controller class do I have a pointer to it? i.e. is there a variable name for it? the UIImageViews I create myself I obviuosly know what they are called.....

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

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

发布评论

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

评论(2

断爱 2024-08-31 08:47:39

您可能将指令放入了错误的初始化程序中。

根据 文档,对象如果从 NIB 中取消归档,并且符合 NSCoding 协议,则使用 initWithCode: 进行初始化;不符合 NSCoding 的对象使用 init 进行初始化。

在这种特殊情况下,UIImageView 确实符合 NSCoding。您可能希望调用 initWithFrame: 并将指令放入该方法中。

You likely have put your instructions in the wrong initializer.

According to the documentation, objects unarchived from a NIB are initialized with initWithCode: if they conform to the NSCoding protocol; objects that don't conform to NSCoding are initialized with init.

In this particular case, UIImageView does conform to NSCoding. It's likely that you have you intended for initWithFrame: to be called and put your instructions in that method.

差↓一点笑了 2024-08-31 08:47:39

你不能简单地将初始化逻辑放在viewDidLoad中吗?尤其,

-(void)viewDidLoad {
  [super viewDidLoad];
  // Put whatever was in your custom initialiser here.
}

Can you not simply put your initialisation logic in viewDidLoad? In particular,

-(void)viewDidLoad {
  [super viewDidLoad];
  // Put whatever was in your custom initialiser here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文