5

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:?

johnMa
  • 3,291
  • 24
  • 37

1 Answers1

2

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.

Taha Selim Bebek
  • 45
  • 1
  • 8
  • 16
  • I know File's owner is important in nib file, but i wonder it's meaning in `[[[NSBundle mainBundle] loadNibNamed: owner: options:`,and why its working in my situation. – johnMa Dec 17 '13 at 03:09
  • 1
    it is the same meaning, is your CustomAlert a subclass of UIAlertView or UIButton? If it is an alertview, how do you set the delegate? If it is a UIButton how do you add target? In both cases if you are doing it programmatically, you don't need the file's owner. – Taha Selim Bebek Dec 17 '13 at 03:18
  • so what you mean is the CustomAlert's File's owner is CustomAlert class because i add the target to CustomAlert class? – johnMa Dec 17 '13 at 03:24
  • no, it does not have a 'files owner' and it does not need one, because you add the target programmatically 'files owner' is not needed. – Taha Selim Bebek Dec 17 '13 at 03:26
  • so your opinion is owner is not necessary when custom a view in xib and user `[[[NSBundle mainBundle] loadNibNamed: owner: options:`to initialize it. – johnMa Dec 17 '13 at 03:41