简体   繁体   English

IB / Xcode 4.5.1和iOS Simulator 6.0定制UIView操作方法

[英]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: 目前,我要做的是:

  1. Create custom view files: File -> New -> File -> Objective-C Class and select type of UIView and name it MyView; 创建自定义视图文件:File-> New-> File-> Objective-C Class,选择UIView的类型并将其命名为MyView; MyView.h and MyView.m are created 创建了MyView.h和MyView.m
  2. Create custom view nib: File -> New -> File -> User Interface -> View and name it 'cool'; 创建自定义视图笔尖:File-> New-> File-> User Interface-> View并将其命名为“ cool”; cool.xib is created cool.xib已创建
  3. 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时加载它,还是需要做更多工作?
  4. Also, in IB, I highlight the File's Owner and set its Custom Class to MyView. 同样,在IB中,我突出显示文件的所有者并将其自定义类设置为MyView。 (? - not sure about this) (?-不确定)
  5. 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。
  6. I have a Single View Application and I would like to have an instance of MyView / cool on it so, in IB, I drag a View from the object inspector to the ViewController.xib view. 我有一个单一视图应用程序,并且想在其上创建MyView实例/酷,因此在IB中,我将一个View从对象检查器拖到ViewController.xib视图。 In the IB Identity Inspector, I set the class to MyView. 在IB身份检查器中,我将类设置为MyView。 I import the MyView.h file into the ViewController.h and set up a property for it in the interface. 我将MyView.h文件导入到ViewController.h中,并在界面中为其设置属性。
  7. In ViewController.m's viewDidLoad, I set the background color to orange and set the myLabel (which is being populated by Xcode so it seems to kinda work). 在ViewController.m的viewDidLoad中,我将背景色设置为橙色,并设置了myLabel(它由Xcode填充,因此似乎可以工作)。

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.

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