iphone dev:UIImageview 子类接口生成器 - 如何调用自定义初始值设定项
我搞乱了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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能将指令放入了错误的初始化程序中。
根据 文档,对象如果从 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 theNSCoding
protocol; objects that don't conform toNSCoding
are initialized withinit
.In this particular case, UIImageView does conform to
NSCoding
. It's likely that you have you intended forinitWithFrame:
to be called and put your instructions in that method.你不能简单地将初始化逻辑放在viewDidLoad中吗?尤其,
Can you not simply put your initialisation logic in viewDidLoad? In particular,