iosuiviewuinib

How to load CustomView with CustomView.xib when specify class in another XIB?


I create CustomView:UIView with CustomView.xib file. Now I want to use it by drag view into another XIB (ex: UIViewController.xib) and choose class: customView.

I can load successful when I init CustomView and addSubView to anotherView:

- (void)viewDidLoad{
    //Success load NIB
    CustomView *aView = [[CustomView alloc] initWithFrame:CGRectMake(40, 250, 100, 100)];
    [self.view addSubview:aView];
}

//CustomView.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSLog(@"INIT");
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        [[nib objectAtIndex:0] setFrame:frame];
        self = [nib objectAtIndex:0];
    }
    return self;
}

In the case reuse CustomCell by drawing it into another XIB and then specify class as CustomView. I know awakeFromNib is called but don't know how to load CustomView.xib. how to do that?

*EDIT:

initWithCoder is also called when specify class but it create a loop and crash with loadNibNamed. Why that?

- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        NSLog(@"Coder");
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"QSKey" owner:nil options:nil];
        [[nib objectAtIndex:0] setFrame:self.bounds];
        self = [nib objectAtIndex:0];
    }
    return self;
}

Solution

  • Directly drag custom xib in your view controller is not possible.But you can do one thing,drag its super class view and set its class to your custom class.And connect its properties to the objects you will place according to your Custom Class.

    You can directly load your custom xib using following code:

    //load xib using code ....

       QSKey *keyView=[[QSKey alloc] init];
       NSArray *topObjects=[[NSBundle mainBundle] loadNibNamed:@"QSKey" owner:self options:nil];
    
    for (id view in topObjects) {
        if ([view isKindOfClass:[QSKey class]]) {
            keyView=(QSKey*)view;
        }
    }
    
    keyView.label.text=@"CView";
    [keyView setFrame:CGRectMake(0, 0, 100, 100)];
    [keyView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:keyView];
    
    here label is UILabel object you have used in your custom class as its property.
    
    
    //Load custom View using drag down objects of type Custom Class Super class.
    
    for (QSKey *aView in self.view.subviews) {
        if ([aView isKindOfClass:[QSKey class]]) {
            [self addGestureRecognizersToPiece:aView];
            aView.label.text=@"whatever!";
        }
    }
    
    Here label is object of UILabel you have to place on your dragged View to view controller's xib view.Change that view's class to your Custom Class.Then it will will show its label property in outlet connect it to your UILabel object placed over dragged view.
    

    Here is a working code TestTouchonView

    And the Screenshot will display as follows.

    enter image description here