当我 addSubview:
时,没有任何显示。我设置文本和颜色来查看它。此外,如果我手动将 View 添加到 xcode 中 UI bulder 中的自定义 View ,它会显示文本和颜色。
.m 文件
- (void)displayString:(NSString *)title {
NSRect frame = NSMakeRect(10, 10, 200, 17);
NSTextfield *newfield = [[NSTextField alloc] initWithFrame:frame];
[newfield setBezeled:NO];
[newfield setDrawsBackground:NO];
[newfield setEditable:NO];
[newfield setSelectable:NO];
[newfield setStringValue:title];
[newfield setTextColor:[NSColor blueColor]];
[test addSubview:newfield];
if([test.subviews containsObject:newfield]){
NSLog(@"view there"); // i get this message
}
if([newfield isHidden]){
NSLog(@"view hidden"); //i dont get this message
}
NSLog(@"view set");
}
test
是我已正确链接的 NSView
(自定义 View 是 xcode 所称的)。
因此,当我创建文本字段并将其手动添加到 NSView
,然后通过添加文本和颜色运行相同的代码时一切正常,当我尝试以编程方式设置 View 时会出现此问题。我还确保这不是我创建的 View ,因为我尝试在构建器中创建 View 而不是将它放在 NSView
中,然后尝试 addSubview:
但这也行不通。如果您需要更多代码,请告诉我。
发展: 如果 nsview(自定义 View )中已经有一个元素(手动添加并且可以是任何元素)并且我添加它工作的文本字段(我在 nsview 中获得两个 View )? subview 已经过测试,只是看不到它。
最佳答案
你必须调用 initWithFrame:
而不仅仅是 init
- (void)displayString:(NSString *)title {
NSRect frame = NSMakeRect(10, 10, 200, 200);
NSTextField *newfield = [[NSTextField alloc] initWithFrame:frame];
[newfield setStringValue:title];
[newfield setTextColor:[NSColor blueColor]];
[test addSubview:newfield];
NSLog(@"view set");
}
关于objective-c - addSubview 没有出现在 NSView Objective C Cocoa 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14115595/