Я хочу загрузить Xib
файл в представление элементов NSMenu. У меня есть пользовательский класс представления с Xib, который имеет метки
@interface CustomItemView : NSView
@property (weak) IBOutlet NSTextField *Label1;
@property (weak) IBOutlet NSTextField *Label2;
@end
@implementation CustomItemView{
}
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
@end
У меня есть класс «Элемент меню», в который я хочу загрузить приведенный ниже блок. Это класс «Элемент меню», в который я хочу загрузить свой пользовательский вид. как мне это сделать?
@interface myMenuItem : NSMenuItem
@end
@implementation myMenuItem{
CustomItemView *customItemView;
}
-(id)initWithDisplayDetails:(CGDirectDisplayID)display {
self = [super init];
if (self) {
customItemView = [[CustomItemView alloc] init];
self.view = customItemView;
self.target = self;
self.action = @selector(changeCustomDisplayMode);
}
return self;
}
Редактировать: По предложению Виллека я реализовал приведенный ниже код, и он работает для меня.
-(id)initWithDisplayDetails:(CGDirectDisplayID)display {
self = [super init];
if (self) {
NSArray *nibObjects = [[NSArray alloc] init];
[[NSBundle bundleForClass:[self class]] loadNibNamed:@"CustomItemView" owner:self topLevelObjects:&nibObjects];
for (id topLevelObject in nibObjects) {
if ([topLevelObject isKindOfClass:[NSView class]]){
CustomItemView * customItemView = (CustomItemView *)topLevelObject;
customItemView.label1.stringValue = @"1value"
customItemView.label2.stringValue = @"2value";
self.view = customItemView;
}
}
self.target = self;
self.action = @selector(changeCustomDisplayMode);
}
return self;
}
Любая помощь наиболее ценится ..