我正在以编程方式创建一个新窗口和视图。我需要视图接受First Responder状态。无论出于何种原因,它都不是。
我正在上这所旧学校,因为我希望我的应用程序与旧版本的OSX兼容。这就是我所拥有的:
- (IBAction)startShow:(id)sender {
// Capture the main display
if (CGDisplayCapture( kCGDirectMainDisplay ) != kCGErrorSuccess) {
NSLog( @"Couldn't capture the main display!" );
}
// Get the shielding window level
windowLevel = CGShieldingWindowLevel();
// Get the screen rect of our main display
screenRect = [[NSScreen mainScreen] frame];
// Put up a new window
displayWindow = [[ShowWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO screen:[NSScreen mainScreen]];
[displayWindow setBackgroundColor:[NSColor blackColor]];
[displayWindow setLevel:windowLevel];
[displayWindow makeKeyAndOrderFront:nil];
// Load show window with options from settings window.
[displayWindow loadOptions:screenRect];
}
为了澄清,displayWindow是ShowWindow的一个实例,它是NSWindow的子类。在我的ShowWindow类中,我正在执行以下操作:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (void) loadOptions:(NSRect)screenRect {
// Create custom view to hold text fields
view = [[ShowView alloc] initWithFrame: screenRect];
[self setContentView:view];
// Track Title Text Field Initialize
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, screenRect.size.height / 4, screenRect.size.width, 350)];
[textField setStringValue:@"My Label"];
[textField setBezeled:NO];
[textField setDrawsBackground:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setTextColor:[NSColor whiteColor]];
[textField setFont:[NSFont fontWithName:@"Helvetica" size:92]];
[textField setAlignment:NSCenterTextAlignment];
// Add track title text field to main view.
[view addSubview:textField];
// Make the view first responder
[self makeFirstResponder:view];
}
view是ShowView的一个实例,它是NSView的子类。最后但并非最不重要的是这是我在ShowView中发生的事情:
// Next four methods set main view as first responder to accept keyboard input
- (BOOL)acceptsFirstResponder
{
NSLog(@"I accepted being a first responder! Yea!");
return YES;
}
- (BOOL)resignFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (BOOL)becomeFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (void)insertText:(NSString *)input
{
}