2017-09-05 55 views
1

我创建了XIBInitWithCoder从来没有要求可可基于NSView的类

我创建了这个类,名为MyCustomView,并将其分配给XIB的文件所有者。

- (instancetype)initWithCoder:(NSCoder *)aDecoder{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) [self load]; 
    return self; 
} 

- (instancetype)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) [self load]; 
    return self; 
} 

- (void)load { 

    NSArray* topLevelObjects; 
    [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" 
           owner:self 
         topLevelObjects:&topLevelObjects]; 

    NSView* view; 
    for (id aView in topLevelObjects) { 
    if ([umaVista isKindOfClass:[NSView class]]) { 
     view = umaVista; 
     break; 
    } 
    } 

    [self addSubview:view]; 
    view.frame = self.bounds; 

} 

我在主应用上创建了一个NSView

我将该视图更改为MyCustomView

我运行该应用程序。 MyCustomViewinitWithCoder不运行。 initWithFrame不运行。 awakeFromNib不运行。

没有任何反应。

任何想法?

+0

你的意思是“将它分配给XIB的第一响应者”?你不能“分配”任何东西给第一响应者,这是代表动态第一响应者值的代理项目。 –

+0

你是什么意思?这就是网络上的所有教程。 – SpaceDog

+0

你的意思是你将它分配给“文件所有者”? –

回答

1

“文件所有者”as I've written elsewhere,不是存档中的真实对象。这是一个占位符。当拆开笔尖包装时,会使用预先存在的物体。

它看起来像你应该把你的自定义视图的实例放入笔尖。不要让它成为File's Owner,只需从对象选​​项板中拖出一个视图,然后在Identity Inspector(右窗格,顶部;按⌘-⌥-3)中更改它的类。在笔尖也建立子视图。

然后让您的NSBundle为您加载笔尖。您的自定义视图将获得initWithCoder:awakeFromNib,您不必通过层次结构来查找特定的子视图。

+0

好的谢谢。我去做! – SpaceDog