What's the meaning of owner when using loadNibNamed?

1.2k views Asked by At

Apple Doc says One of the most important objects in a nib file is the File’s Owner object , but it seems that's the File's owner in nib file , not the one set by LoadNibName method. I wonder what's the difference between them?

Here is an example:

I customize an alertView from xib and offer a static class method like this:

+(CustomAlert *)sharedAlert{
    CustomAlert *alert = [[[NSBundle mainBundle] loadNibNamed:@"CustomAlert" owner:nil options:nil]lastObject];
    return alert;
}

and I have a method to show alert on view

- (void)showInView:(UIView *)view{
    [view addSubview:self];
}

and in my viewController:

- (IBAction)buttonPressed:(id)sender{
    CustomAlert *alert = [CustomAlert sharedAlert];
    [alert showInView:self.view];
}

it works well in my situation,so is it necessary to set the owner in [[[NSBundle mainBundle] loadNibNamed: owner: options:?

1

There are 1 answers

5
Taha Selim Bebek On

Let's say you have nib file with one tableview and the tableviews delegate and datasource is hooked up to "files owner" in interface builder. If you set files owner to any object, then that object will be the datasource and delegate of the tableview. This is valid for anything hooked up to the files owner.