可重用的接口位,在IB中设计 [英] Reusable bits of interface, designed in IB

查看:86
本文介绍了可重用的接口位,在IB中设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,它在许多不同的上下文中包含相同的按钮组。按钮将其操作发送到每个上下文中的不同对象。我想能够在IB中设计一个单独的NSView包含按钮,然后能够把这个视图的副本在我的笔记本电脑的许多地方,同时保持链接,所以更改传播。我想将每个实例连接到不同的对象,并且按钮将其操作发送到父视图连接的任何对象。

I'm making an app that includes the same group of buttons in many different contexts. The buttons send their actions to a different object in each context. I'd like to be able to design a single NSView in IB containing the buttons, and then be able to put copies of that view in many places in my nibs, while maintaining the link, so changes propagate. I'd like to connect each of those instances to different objects, and have the buttons send their actions to whatever object their parent view is connected to.

我想创建NSView的子类,当加载时,用从nib文件加载的另一个视图替换它自己,将连接的对象设置为文件的所有者,但我不相信这是最干净的方法。这是我的实现这个想法(其中工作):

I thought of creating a subclass of NSView which, when loaded, replaces itself with another view which it loads from a nib file, setting the connected object as File's Owner, but I'm not convinced this is the cleanest method. Here's my implementation of that idea (which -does- work):

@implementation AVNViewFromNib

- (void)awakeFromNib
{
    //Load the nib whose name is specified by the "nibFile" key
    NSNib* viewNib = [[NSNib alloc] initWithNibNamed:[self valueForKey:@"nibFile"] bundle:[NSBundle mainBundle]];
    NSMutableArray* topLevelObjects = [NSMutableArray new];
    [viewNib instantiateNibWithOwner:relatedObject topLevelObjects:&topLevelObjects];

    //Find our replacement view in that nib
    for (id currentObject in topLevelObjects)
    {
        if ([currentObject isKindOfClass:NSClassFromString(@"AVNReplacementView")])
        {
            representedView = currentObject;
            break;
        }
    }

    //Copy appropriate properties from us to our representedView
    [representedView setAutoresizingMask:[self autoresizingMask]];
    [representedView setFrame:[self frame]];
    [[self superview] addSubview:representedView];

    //We were never here. :)
    [self removeFromSuperview];
    [viewNib autorelease];
}

@end

@implementation AVNReplacementView
@end

这是最干净的方法吗?

Is that the cleanest method? Is there a standard way of going about this?

推荐答案

您可以使用IB中的按钮创建视图,然后拖动该视图进入库窗口并保存。捕获是,他们之间没有链接;编辑一个不会改变任何关于其他人。

You can create the view with the buttons in it in IB, then drag that view into the Library window and save it. The catch is, there's no "link" between them; editing one won't change anything about the others.

如果你想要,你需要做一个NSView的子类。

If you want that, you'll need to make a subclass of NSView instead.


我想到创建一个NSView的子类,当加载时,将它自己替换为另一个视图,它从一个nib文件加载,将连接的对象设置为文件的所有者,但我不相信这是最干净的方法。

I thought of creating a subclass of NSView which, when loaded, replaces itself with another view which it loads from a nib file, setting the connected object as File's Owner, but I'm not convinced this is the cleanest method.

这可以工作。我不认为这是真的那么脏;原因 init 方法返回一个对象是他们显式地可以返回一个不同的对象。但是,我不知道如何处理不同框架的视图,因为加载的视图将具有它在nib中的任何框架。

That could work. I don't think that's really all that dirty; the reason init methods return an object is that they explicitly can return a different object. However, I'm not sure how you'd handle views of different frames, since the loaded view will have whatever frame it has in the nib.

另一种方法是从笔尖加载按钮,但您必须先调整其框架,然后再将它们添加为子视图。

Another way would be to load the buttons from a nib, but you'd have to adjust their frames before adding them as subviews.

这篇关于可重用的接口位,在IB中设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆