Cocoa:将子视图添加到来自不同类和笔尖的视图

发布于 2024-12-22 09:42:01 字数 153 浏览 6 评论 0原文

如果我有两个带有多个视图的笔尖,有没有办法在它们之间使用 addSubview: 方法?我想做的是从其中一个笔尖获取视图,并告诉它添加一个子视图,该子视图将是另一个笔尖文件中的视图。

我将它们放在单独的笔尖中的原因是因为第二个笔尖的子视图将使用相同的模板但不同的参数添加多次。

If i have two nibs with several views, is there a way for me to use the addSubview: method between them? What I would like to do is take a view from one of the nibs and tell it to add a subview that would be a view in the other nib file.

The reason I have them in separate nibs is because the subview from the second nib will be added several times, using the same template but different parameters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2024-12-29 09:42:01

是的,您可以将一个笔尖中的视图作为子视图添加到另一笔尖中的视图。

您需要创建一个拥有子笔尖的 NSViewController 对象。因此,一旦初始化视图控制器,与其关联的笔尖就会被加载。现在您可以使用控制器的 view 属性并将其作为子视图添加到任何其他视图。

下面的代码将帮助您更好地理解:

YourViewController.m

-(id)init 
{    
    self = [super init];

     if(nil != self)
     {
         [NSBundle loadNibNamed:@"myNibName" owner:self];
     }

     return self; 
}

YourOtherClass.m

-(void)addYourViewControllerViewAsSubview
{ 

    YourViewController *yvc = [[YourViewController alloc] init];

    [yourOtherViewOutlet addSubview:yvc.view];

}

Yes, you can add a view in one nib as a subview to the view in another nib.

You need to create a NSViewController object which will own the child nib. So that as soon as you initialize the view controller the nib associated with it is loaded. Now you can use the view property of the controller and add it as a subview to any other view.

The code below will help you understand better:

YourViewController.m

-(id)init 
{    
    self = [super init];

     if(nil != self)
     {
         [NSBundle loadNibNamed:@"myNibName" owner:self];
     }

     return self; 
}

YourOtherClass.m

-(void)addYourViewControllerViewAsSubview
{ 

    YourViewController *yvc = [[YourViewController alloc] init];

    [yourOtherViewOutlet addSubview:yvc.view];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文