开发者

can't edit NSTextField in sheet at runtime

开发者 https://www.devze.com 2023-04-08 00:56 出处:网络
When clicking on a button, I\'d like to display a sheet with an email+password prompt with options to save and cancel. The UI is all set up, the actions are in place, and the sheet appears and cancels

When clicking on a button, I'd like to display a sheet with an email+password prompt with options to save and cancel. The UI is all set up, the actions are in place, and the sheet appears and cancels as expected. The problem is that I can't edit either of the NSTextFields at runtime; they appear to disabled, and the OS error sound plays on every key press when the sheet is open. I read on SO that UIActionSheet is appropriate, but this is not an iOS app.

The textfields are enabled, and had previously been working in another panel. I made sure that the IBAction links are intact, but I'm otherwise not even sure how to troubleshoot.

What about a sheet would cause an otherwise healthy NSTextField to refuse input?

// show the sheet
-(IBAction)showAccount:(id)sender {
    [NSApp beginSheet:accountWindow 
       modalForWindow:prefsWindow
        modalDelegate:self 
       didEndSelector:NULL 
          contextInfo:NULL];
}

// cancel/hide the sheet
-(IBAction)cancelAccount:(id)sender {
    [NSApp endSheet:accountWindow];
    [accountWindow orderOut:nil];   
}

Edit: I've just discovered that I can right-click and paste text into each field, but I can't select or delete. It seems like the NSTextFields aren't getting focus and don't receive keyboard input like they usually would. I also forgot to mention that my Save butto开发者_如何学Pythonn calls and executes its related method properly.


It turns out that I haphazardly found the solution, which I will now post for posterity...

The view that gets used as the sheet (NSWindow or NSPanel) needs to have a title bar. As soon as I toggled this in Interface Builder's inspector (Window → Appearance → Title Bar checkbox), I recompiled and the NSTextFields highlighted, tabbed, and accepted input like they would in any other view. Not sure why the title bar makes a difference, but there you have it.


The view does not absolutely have to have a title bar.

See Why NSWindow without styleMask:NSTitledWindowMask can not be keyWindow?

Which states: If you want a titleless window to be able to become a key window, you need to create a subclass of NSWindow and override -canBecomeKeyWindow as follows:

- (BOOL)canBecomeKeyWindow {
    return YES;
}

This worked for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号