我想将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
我有一个要在其中加载以下菜单项的Menu Item类,是我要在其中加载自定义视图的菜单菜单Item类。我该怎么办?
@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;
}
编辑:根据Willeke的建议,我已经实现了以下代码,并且对我有用。
-(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;
}
最感激的帮助。