Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't edit NSTextField in sheet at runtime

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 button calls and executes its related method properly.

like image 847
Matt Stein Avatar asked Sep 26 '11 21:09

Matt Stein


2 Answers

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.

like image 151
Matt Stein Avatar answered Nov 02 '22 11:11

Matt Stein


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.

like image 42
Sofi Software LLC Avatar answered Nov 02 '22 09:11

Sofi Software LLC