简体   繁体   中英

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.

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. 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).

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 in advance

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?

It knows that your XIB is managed by that class. 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.

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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