can't edit NSTextField in sheet at runtime

核能气质少年 提交于 2019-11-28 22:13:31

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7561347/cant-edit-nstextfield-in-sheet-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!