2

This is another "I'm confused question". So I'm working on bringing in custom views into a view controller. I'll just outline the exact steps for the error.

  1. Create a Single View Application Project
  2. Create a Nib File via File -> New -> User Interface -> View; call it theNIB.xib. Add a simple label to make sure loading.
  3. add following code:

    UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"theNIB" owner:self options:nil] objectAtIndex:0];
    view.frame=CGRectMake(10.0f,10.0f,100.0f,100.0f);
    view.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:view];
    

This works.

Now what I want to do is connect this nib with a UICustomView so I create ArcView via File -> New -> UIView.

'4. In IB, I need to connect theNIB to ArcView so I highlight the File's Owner in the Placeholders and select AcrView in the Custom Class.

enter image description here

'5.' Then I select the main View and set it to ArcView in the Custom Class.

enter image description here

I am at a loss for what the next step is or whether 4 or 5 were necessary (both / neither)? Do I try to make an outlet reference in ArcView to view for the main View in Interface Builder? Should I be able to Alt-drag from the View to the header file in assistant editor (I am not currently able to)?

thx in advance

** edit 1 **

Here's the File's Owner with arcView set:

enter image description here

View object with arcView not set:

enter image description here

timpone
  • 19,235
  • 36
  • 121
  • 211

2 Answers2

1

Define an IBOutlet @property in your parent class's @interface section like this:

@property (weak, nonatomic) IBOutlet ArcView *arcView

Then go into Interface Builder, and right click on File's Owner. When you see "arcView" in the black HUD window, drag the mouse from that item to your view on the XIB.

Now you have a property for your arcview control, and you can utilize it just like you would any control such as UIButton, UILabel etc.

melsam
  • 4,947
  • 1
  • 22
  • 23
  • 1
    thx - I updated but getting it to crash with `2013-02-28 22:54:24.066 Sam[22922:11303] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key arcView.'` See edit #1 above for screenshots – timpone Mar 01 '13 at 06:58
1

Set the file's owner == your UIView subclass so that you can connect outlets to it. And you should set the class of nib-painted UIView to that same subclass because it's an instance of that UIView subclass.

In other words, follow these steps:

  1. create a UIView subclass called CustomView
  2. create a UIView xib New File -> User Interface -> View
  3. change the file's owner to CustomView
  4. change the view's class to CustomView
  5. add subviews if you wish, connect them as outlets to the files owner (CustomView)

Your crash is happening because your code says this:

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"theNIB" owner:self options:nil] objectAtIndex:0];

But that owner:self is the view controller where this code is running. You want the view subclass to be the nib's owner.

To fix, give your UIView subclass the job of init-ing itself from the nib, like this:

CustomView.h

@interface CustomView : UIView

- (id)initFromNib;

@end

CustomView.m

#import "CustomView.h"

@interface CustomView ()
// connect this in the XIB to file's owner that you've set to this CustomView class
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end

@implementation CustomView

- (id)initFromNib
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil] objectAtIndex:0];

    if (self) {
        // prove you can set properties on your outlets
        self.myLabel.text = @"this is good";
    }
    return self;
}

I built a little project with just this stuff in it as described. Works fine. Lemme know if you'd like to see it and I'll find a way to send you an anonymous zip.

danh
  • 62,181
  • 10
  • 95
  • 136
  • I think this is the best explanation for understanding difference between setting File's Owner and Class in XIB. I have gone through so many question and answers regarding these but unable to understand difference clearly. But, this is very clear to me. Specially steps and code written by @danh. This answer deserves so many upvotes. Thank you very much. – Nitesh Borad Jun 13 '17 at 05:41
  • what is the point of change the view's class to CustomView after setting the owner? – nrudnyk Apr 23 '18 at 14:37
  • @nrudnyk, the view's class in the nib determines the class of the object that gets instantiated when the nib is loaded. – danh Apr 23 '18 at 14:53