多くの異なるコンテキストで同じボタンのグループを含むアプリを作成しています。ボタンは、コンテキストごとに異なるオブジェクトにアクションを送信します。ボタンを含む単一の NSView を IB で設計し、リンクを維持しながらペン先の多くの場所にそのビューのコピーを配置できるようにして、変更が反映されるようにしたいと考えています。これらの各インスタンスを異なるオブジェクトに接続し、ボタンのアクションを親ビューが接続されているオブジェクトに送信したいと思います。
NSView のサブクラスを作成することを考えました。ロードされると、nib ファイルからロードされる別のビューに置き換えられ、接続されたオブジェクトがファイルの所有者として設定されますが、これが最もクリーンな方法であるとは確信していません。これがそのアイデアの私の実装です(これは機能します):
@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
それは最もクリーンな方法ですか?これについて標準的な方法はありますか?