2

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;
}
LE SANG
  • 10,955
  • 7
  • 59
  • 78
  • Not getting you.. what you want actually?? – Anil Varghese Feb 19 '13 at 09:57
  • I want load CustomView together with its xib but not use initWithFrame, just drag view to another Xib and then specify class.But the XIB not load. – LE SANG Feb 19 '13 at 10:01
  • I think this is what you want to do, no? http://stackoverflow.com/a/5096476/840428 – followben Feb 19 '13 at 10:12
  • @followben Thanks!Same as my question.But I do the same thing in initWithCoder and it create a loop and my app crash!still investigate! – LE SANG Feb 19 '13 at 10:29
  • That's because you're still trying to set self to the object instantiated in the nib, which will in turn instantiate a new instance, which will try to set self to the object instantiated in the last nib... and so on in an infinite loop. In the answer I linked to, notice he's pulling out the object and setting it as a subview, not setting it to self. – followben Feb 19 '13 at 11:04
  • Only 1line cause loop: NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]; I comment out setting view below but nothing change – LE SANG Feb 19 '13 at 15:33

1 Answers1

1

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

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90