iOS SDK using loadNibNamed, code within 'init" not working as expected

2.5k views Asked by At
FluidInfo *fluidInfo = [[FluidInfo alloc]init];
UIView *info = [[[NSBundle mainBundle] loadNibNamed:@"FluidInfoSheet" owner:fluidInfo options:nil] objectAtIndex:0];

[self createFormulaPopup:info];

I have a nib file with a UIView. and I have a subclass of UIView called 'FluidInfo.' I make this UiView appear as a popup inside my viewController using my function 'createFormulaPopup'

I have made my UIView a subclass of FluidInfo. When I create outlets and actions they are all working correctly.

The problem is that my init function within my UIView is working unexpectedly. If I log something then it appears at the same time as my popup.. but if i set something like background color of the view it seems to disappear. If I set it in interface builder it will stick. If I attach the change of the color to an action within UIView then that will work as well. But when the view is initialized it seems to undo all the stuff I've done programmatically.

2

There are 2 answers

0
hamobi On

okay so i fixed this. i loaded the nib within the uiview itself instead of within my viewcontroller

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"FluidInfoSheet" owner:self options:nil]objectAtIndex:0];

        // Initialization code
    }
    return self;
}
0
user1548843 On

in your code you pass FluidInfo *fluidInfo instated of your view Controller

 [[[NSBundle mainBundle] loadNibNamed:@"FluidInfoSheet" owner:fluidInfo options:nil]objectAtIndex:0];

refer this link https://developer.apple.com/library/ios/documentation/uikit/reference/NSBundle_UIKitAdditions/Introduction/Introduction.html

in that given you have to pass owner that would be your file owner here you pass owner as view subclass that's why it may not work pass self (your view controller)so it would find file owner in application.

  [[[NSBundle mainBundle] loadNibNamed:@"FluidInfoSheet" owner:self options:nil]objectAtIndex:0];

refer this link of Stack over flow give you very good description of file owner What describes the "File's Owner" best in objective-c / cocoa Nib?