苹果文档称在nib文件中最重要的对象之一是File's Owner,但似乎这是nib文件中的File's owner,而不是由LoadNibName
方法设置的那个。我想知道它们之间有什么区别?
下面是一个例子:
我从xib自定义一个alertView并提供了一个静态类方法:
+(CustomAlert *)sharedAlert{
CustomAlert *alert = [[[NSBundle mainBundle] loadNibNamed:@"CustomAlert" owner:nil options:nil]lastObject];
return alert;
}
我有一种方法可以在视图上显示警报
- (void)showInView:(UIView *)view{
[view addSubview:self];
}
在我的视图控制器中:
- (IBAction)buttonPressed:(id)sender{
CustomAlert *alert = [CustomAlert sharedAlert];
[alert showInView:self.view];
}
在我的情况下它很好用,那么在 [[[NSBundle mainBundle] loadNibNamed: owner: options:
中设置所有者是否必要呢?
[[[NSBundle mainBundle] loadNibNamed: owner: options:]
中的含义,以及为什么它在我的情况下起作用。 - johnMa[[[NSBundle mainBundle] loadNibNamed: owner: options:
来初始化它。 - johnMa