1

I have a custom UIView which I am loading in this manner

- (id)initWithFrame:(CGRect)frame withDelegate:(id)del
{
    self = [super initWithFrame:frame];
    UIView *myView;

    if (self)
    {
        NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                                      owner:self
                                                    options:nil];
        for (id object in nibViews) 
        {
            if ([object isKindOfClass:[self class]])
            myView = object;
        }

        myView.frame = frame;
        myView.backgroundColor = [UIColor clearColor];

        [self addSubview: myView];
    }

    self.delegate = del;
    return self;
}

On loading the view like this when UIView method returns self all the outlets linked in the Interface Builder are nil. Is it not the right way ?

Avinash Sharma
  • 665
  • 1
  • 7
  • 23
  • You don't need `for-loop` just use `myView = [nibViews firstObject];` – iphonic May 03 '17 at 12:32
  • 1
    Possible duplicate of [How to load a xib file in a UIView](http://stackoverflow.com/questions/7814928/how-to-load-a-xib-file-in-a-uiview) – IPS Brar May 03 '17 at 12:35
  • Please do share demo code if possible. – Hitesh Surani May 03 '17 at 13:18
  • Possible duplicate of [question about init and load from xib for a custom UIView](http://stackoverflow.com/questions/7191959/question-about-init-and-load-from-xib-for-a-custom-uiview) – Sargis May 03 '17 at 14:04

4 Answers4

0

Use this method for load particular xib from bundle

UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
0

Try this :

 self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                              owner:self
                                            options:nil] objectAtIndex:0];
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

Use this

myView =[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil].firstObject];

Or simple you can use

myView = [nibViews firstObject]
Lalit kumar
  • 1,797
  • 1
  • 8
  • 14
0

As I understand you provide IBOutlet in the xid and when you try to load the view like you provide these values is nil. This is anticipated becouse you create onew viwe and add the view from xib as subview. So the parrent view does not set this values. to fixe this you need to rework method as loaded your view. Please try use this:

+ (id)createWithFrame:(CGRect)frame withDelegate:(id)del
{


    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                                      owner:nil
                                                    options:nil];
    <Provide class name> *myView = nibViews[0];
    myView.frame = frame;
    myView.backgroundColor = [UIColor clearColor];

    myView.delegate = del;
    return myView;
}
Sergey
  • 1,589
  • 9
  • 13