最も簡単な解決策はMyView、単一のサブビューを作成することです。これは、残りの子孫ビューのコンテナーです。MyVC.xibこれにより、 にあるものと にあるものとの間の単一の接点が得られMyView.xib、両方の xib のアウトレットを接続できます。
でMyVC.xib、各プレースホルダ ビューのクラスを に設定しMyViewます。
でMyView.xib、最上位ビューのクラスを に設定しUIViewます。File's Owner のクラスを に設定しMyViewます。MyViewで接続されていたコンセントが にあるMyView.xib場合は、最上位ビューにそれらのコンセントがなくなっているため、それらをファイルの所有者に再接続する必要があります。
で、最上位ビューをサブビューとして-[MyView initWithCoder:]ロードして追加します。MyView.xibテストされていない例:
+ (UINib *)nib {
static dispatch_once_t once;
static UINib *nib;
dispatch_once(&once, ^{
nib = [UINib nibWithNibName:NSStringFromClass(self) bundle:[NSBundle bundleForClass:self]];
});
return nib;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
NSArray *contents = [[self.class nib] instantiateWithOwner:self options:nil][0];
UIView *containerView = contents[0];
// Make sure the container view's size tracks my size.
containerView.frame = self.bounds;
containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.autoresizesSubviews = YES;
if ([self respondsToSelector:@selector(setTranslatesAutoresizingMaskIntoConstraints:)]) {
self.translatesAutoresizingMaskIntoConstraints = YES;
containerView.translatesAutoresizingMaskIntoConstraints = YES;
}
// If you're using autolayout in both xibs, you should probably create
// constraints between self and containerView here.
[self addSubview:containerView];
}
return self;
}
MyViewこれには、のアウトレットを のモノと のモノにMyVC.xib接続できるという効果がありMyView.xib、 の他のオブジェクトのアウトレットとMyVC.xibのMyView.xibインスタンスに接続できますMyView。MyVC.xibただし、他のオブジェクトのアウトレットを他のオブジェクトに、MyView.xibまたはその逆に接続することはできません。