[英]custom UIView how-to with IB / Xcode 4.5.1 and iOS Simulator 6.0
I know that there are several questions similar to this but I am finding that most of them don't work with current IB / iOS or are not complete. 我知道有几个与此类似的问题,但是我发现其中大多数问题不适用于当前的IB / iOS或尚未完成。
I want to have a custom UIView that has a nib but am failing of getting this hooked up
properly. 我想要一个具有笔尖的自定义UIView,但是无法正确hooked up
。 I have spent several hours debugging and off to SO I go (and maybe sit in bathtub). 我花了几个小时进行调试,然后才去(可能坐在浴缸里)。 Apologies in advance if you feel that this is a simple operation; 如果您觉得这很简单,请提前道歉; thx in advance for any help. 提前寻求任何帮助。
To do this currently, I: 目前,我要做的是:
I run the app and the backgroundColor is set to orange but no label and am cofused as to why not? 我运行该应用程序,并且backgroundColor设置为橙色,但没有标签,并且对为什么不感到困惑? Are any of the above steps incorrect? 以上步骤是否正确?
thx in advance 提前
In IB for cool.xib, go into Identity Inspector and set the Custom Class from UIView -> MyView; 在IB中的cool.xib中,进入Identity Inspector并从UIView-> MyView设置自定义类; Question #1 - at this point does MyView know to load cool.xib when it is instantiated or does more need to be done? 问题#1-此时,MyView是否知道在实例化cool.xib时加载它,还是需要做更多工作?
It knows that your XIB is managed by that class. 它知道您的XIB由该类管理。 But more is needed to be done, in order for you to use that. 但是还需要做更多的工作,以便您使用它。
Skip the 4th question. 跳过第四个问题。 That would work for a UIViewController
, where you define who is going to responsible for it, with the File's Owner. 这将适用于UIViewController
,您可以在其中定义谁来负责文件的所有者。
I want to create a text label so I drag out a UILabel onto cool.xib and create a public property (myLabel) in MyView.h. 我想创建一个文本标签,所以我将UILabel拖到cool.xib上,并在MyView.h中创建一个公共属性(myLabel)。 Question #2 Do I need to set the custom class for this to MyView? 问题#2是否需要为此将自定义类设置为MyView? I would think not as this is just a standard UILabel. 我认为不会,因为这只是一个标准的UILabel。
If you want to be able to access that UILabel from your MyView
, you should create an IBOutlet in your MyView
and link from the root UIView's (that is a sub-class of your MyView
) to the UILabel
. 如果您希望能够从MyView
访问该UILabel,则应该在MyView
创建一个IBOutlet,并将其从根UIView的链接(这是MyView
的子类)链接到UILabel
。 In this case, you don't touch the File's Owner. 在这种情况下,您无需触摸文件的所有者。
Finally check this, in order for you load your custom view properly => Loading custom UIView from nib, all subviews contained in nib are nil? 最后检查一下,为了正确加载自定义视图=> 从nib加载自定义UIView,nib中包含的所有子视图均为nil?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.