简体   繁体   English

iPhone开发人员:UIImageview子类接口生成器-如何调用自定义初始化程序

[英]iphone dev: UIImageview subclass interface builder - how to call custom initializer

I messing with iphone developement. 我搞砸了iPhone的发展。 I have a uiimageview subclass that I want to use to detect touches. 我有一个uiimageview子类,我想用它来检测触摸。 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. 我已经成功添加到interfacebuilder中,并且可以在应用程序的UIImageview子类中检测到触摸,因此在这方面一切都很好。 however my UIImageView subclass has a custom initializer which is not called when it is created in interface builder. 但是我的UIImageView子类有一个自定义初始化程序,在接口构建器中创建它时不会调用它。

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. 如果我手动初始化UIImageview并以编程方式添加它,我认为它将起作用,但随后我将无法“看到”我在“界面”构建器中的位置。

how can I either 我怎么能

1) 'see' a uiimageview in interface builder that is added in code? 1)在代码中添加的接口生成器中“看到” uiimageview? (not possible?) 2) call my custom initializer when the subclass is instantiated in interfacebuilder. (不可能吗?)2)在interfacebuilder中实例化子类时,调用我的自定义初始化程序。

thanks 谢谢


Hi thanks for suggestions. 嗨,谢谢你的建议。 I think I'm getting closer to understanding the relationship between the xib and the viewcontroller. 我想我越来越接近了解xib和viewcontroller之间的关系。

I now am sucessfully adding my UIImageView subclass programmatically and using my custom initiializer which overrides InitWithFrame. 现在,我成功地以编程方式添加了我的UIImageView子类,并使用了覆盖InitWithFrame的自定义初始化程序。

I think I read that the xib calls 'awakeFromNib' so I could equally add my iniitialization code in there. 我想我读过xib叫'awakeFromNib',所以我可以同样在其中添加初始化代码。 I like the idea of adding it programmatically as I have more control (although harderto set up my IU) 我喜欢以编程方式添加它的想法,因为我有更多的控制权(尽管很难设置IU)

so one more realted question. 所以还有一个更现实的问题。 if I add an UIImageView subclass in interface builder. 如果我在界面生成器中添加UIImageView子类。 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? 如果我想在视图控制器类中引用它,是否有指向它的指针? ie is there a variable name for it? 即有一个变量名吗? the UIImageViews I create myself I obviuosly know what they are called..... 我自己创建的UIImageViews我很清楚地知道它们的名字.....

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; 根据文档 ,从NIB取消归档的对象使用initWithCode:如果它们符合NSCoding协议; objects that don't conform to NSCoding are initialized with init . 不符合NSCoding使用init

In this particular case, UIImageView does conform to NSCoding . 在这种情况下,UIImageView确实符合NSCoding It's likely that you have you intended for initWithFrame: to be called and put your instructions in that method. 您可能打算让initWithFrame:被调用并将您的指令放在该方法中。

Can you not simply put your initialisation logic in viewDidLoad? 您是否可以简单地将初始化逻辑放入viewDidLoad中? In particular, 尤其是,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM