xibファイルの内容を取得するには、最初にファイルをロードしてloadNibNamed:owner:options:
、NSBundleクラスにメッセージを送信する必要があります。
CustomViewおよびCustomView.xibファイルという名前のUIViewサブクラスがあるとします。xibファイルでは、各ビューにタグがあります。.hファイルは次のようになります。
@interface CustomView : UIView
@property (nonatomic, assign) UILabel *someTextLabel; //use assign in order to not to override dealloc method
@end
.m
@implementation CustomView
- (id)init {
self = [super init];
if (self) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil];
[self addSubview:[topLevelObjects objectAtIndex:0]]; //this object is a CustomView.xib view
self.someTextLabel = (UILabel *)[self viewWithTag:5]; //consider you have a UILabel on CustomView.xib that has its tag set to 5
}
return self;
}
@end
これは、カスタムUIViewサブクラスに.xibsを使用する方法についてです。アプリがチャットのようなものである場合は、プログラムで追加する必要があります。
2つのカスタムビュー間でメッセージを送信する最良の方法については、それぞれで相互に弱参照を作成する必要があります。
1つに
@property (nonatomic, assign) CustomView *customView;
別の
@property (nonatomic, assign) AnotherCustomView *anotherCustomView;
何かが起こったときにメッセージを送るだけです
- (void)buttonPressed {
[customView handleButtonPressedEvent];
}
これが明らかかどうか教えてください。