34

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Matt Stein
  • 3,053
  • 1
  • 19
  • 35

2 Answers2

83

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.

Matt Stein
  • 3,053
  • 1
  • 19
  • 35
  • Thanks a ton for this answer, I've been having the exact same issue except with an NSTokenField. I tried rewriting everything, redoing everything in IB, and finally found that the title bar was the issue. I wish this issue was better documented. – Keshav Saharia Apr 29 '12 at 01:45
  • I too have been having this issue! I was trying to use an invisible transparent window to do screen overlays with text fields... To my chagrin the text fields were not accepting input... This explains why! Thanks a ton! – Georges Oates Larsen Nov 09 '12 at 17:22
  • I had a similar issue where the default NSButton on the sheet wasn't pulsing. This solved it. –  Apr 05 '13 at 17:09
  • Well, I've readed more about that thing… It's not a bug, it's the way it works but it makes sense to remove the titlebar in a window that will be used as a sheet. Returning YES to canBecomeKeyWindow can be another solution but I tend to not subclass when not necessary. Sorry for the double message but I was late to modify the previous response and I wanted to state that it's not a bug. – Sergi Ramón Jul 05 '13 at 08:39
  • The -canBecomeKeyWindow override appears to be more correct, but this answer here is more practical :)) – Jerry Krinock Mar 24 '16 at 15:41
  • Thanks @JerryKrinock, that's only because my Mac development efforts are rather rudimentary :) – Matt Stein Mar 24 '16 at 18:28
42

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.

Community
  • 1
  • 1
Sofi Software LLC
  • 3,879
  • 1
  • 36
  • 34
  • This worked for me, and made it easier as I didn't have to recode because of a title bar. – epetousis Oct 31 '12 at 05:24
  • 1
    This is the better answer. I don't know why they made this the default behavior for title-less windows and non-controllable via delegate, but I used a subclass with this one method and things worked fine. – Chris R Nov 19 '12 at 17:58
  • This *can* be made default in your application via a category. See [this answer](http://stackoverflow.com/questions/7214273/nstextfield-on-nspopover) – cbedrosian Apr 29 '14 at 01:58
  • In Swift, there is no autocompletion to override canBecomeKeyWindow, but subclassing NSWindow and implementing this function works like a charm. Thanks a lot. – Blackus Apr 02 '15 at 09:30