本文介绍了使用IB / Xcode 4.5.1和iOS Simulator 6.0定制UIView方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几个类似的问题,但我发现他们中的大部分都不适用于当前的IB / iOS或者不完整。

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.

我希望有一个自定义的UIView,它有一个nib但是没有正确地连接连接。我已经花了几个小时调试和关闭所以我去(也许坐在浴缸里)。如果您觉得这是一个简单的操作,请提前道歉;请提前获取任何帮助。

I want to have a custom UIView that has a nib but am failing of getting this hooked up properly. 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. 创建自定义视图文件:文件 - >新建 - >文件 - >目标-C类并选择UIView类型并将其命名为MyView;创建了MyView.h和MyView.m

  2. 创建自定义视图nib:文件 - >新建 - >文件 - >用户界面 - >查看并命名为'酷'; cool.xib已创建

  3. 在IB for cool.xib中,进入Identity Inspector并从UIView设置自定义类 - > MyView;问题#1 - 此时MyView知道在实例化时加载cool.xib还是需要做更多的事情?

  4. 另外,在IB中,我突出显示文件所有者并设置它的自定义类到MyView。 (? - 不确定这个)

  5. 我想创建一个文本标签,所以我将UILabel拖到cool.xib上并在MyView.h中创建一个公共属性(myLabel)。问题#2我是否需要将此自定义类设置为MyView?我想不会因为这只是一个标准的UILabel。

  6. 我有一个单视图应用程序,我想在它上面有一个MyView / cool的实例,所以,在IB中,我拖动a从对象检查器到ViewController.xib视图的视图。在IB Identity Inspector中,我将类设置为MyView。我将MyView.h文件导入ViewController.h并在界面中为它设置属性。

  7. 在ViewController.m的viewDidLoad中,我将背景颜色设置为橙色并设置myLabel(由Xcode填充,因此它似乎有点工作)。

  1. Create custom view files: File -> New -> File -> Objective-C Class and select type of UIView and name it MyView; MyView.h and MyView.m are created
  2. Create custom view nib: File -> New -> File -> User Interface -> View and name it 'cool'; cool.xib is created
  3. In IB for cool.xib, go into Identity Inspector and set the Custom Class from 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?
  4. Also, in IB, I highlight the File's Owner and set its Custom Class to 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. Question #2 Do I need to set the custom class for this to MyView? I would think not as this is just a standard 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. In the IB Identity Inspector, I set the class to MyView. I import the MyView.h file into the ViewController.h and set up a property for it in the interface.
  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).

我运行应用程序并将backgroundColor设置为橙色但没有标签,并且为什么不是?以上任何步骤都是错误的吗?

I run the app and the backgroundColor is set to orange but no label and am cofused as to why not? Are any of the above steps incorrect?

提前thx

推荐答案

它知道你的XIB是由该类管理的。但是需要做更多的事情,以便你使用它。

It knows that your XIB is managed by that class. But more is needed to be done, in order for you to use that.

跳过第4个问题。这适用于 UIViewController ,您可以使用文件所有者定义谁将对其负责。

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.

如果你想能够从那里访问那个UILabel您的 MyView ,您应该在 MyView 中创建一个IBOutlet,并从根UIView的链接(这是一个子类)您的 MyView )到 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. 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?

这篇关于使用IB / Xcode 4.5.1和iOS Simulator 6.0定制UIView方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:42