我有一个模态表,其中包含一个 NSTextField 控件、一个“确定”按钮和一个“取消”按钮。 OK 按钮绑定(bind)到我的 Controller 类中名为 theSheetOK 的操作方法。我还将 NSTextField 控件绑定(bind)到 Controller (文件所有者)中名为 foo 的 NSString 成员,并使用键值绑定(bind)来读取文本用户输入的值(即绑定(bind)检查器中文本字段的模型键路径设置为 foo)。
如果输入文本并且用户通过键盘点击“确定”按钮,则一切正常。当我在 theSheetOK 处理程序中使用 NSLog 跟踪 foo 的值时,我看到了我刚刚在文本字段中输入的值。
但是,当我用鼠标单击“确定”按钮时,foo 的值被记录为空,而且一旦我单击“确定”按钮,文本字段控件就会捕获焦点,并且文本我键入的内容显示为已选择。有什么想法出了什么问题吗?
@interface MyController : NSWindowController {
@private
NSString *foo;
}
@property (copy, readwrite)NSString* foo;
-(IBAction) theSheetOK:(id)sender;
-(IBAction) theSheetCancel:(id)sender;
@end
...
#import "MyController.h"
@implementation MyController
@synthesize foo;
-(IBAction) theSheetOK:(id)sender
{
NSLog(@"theSheetOK");
NSLog(@"foo= %@", foo);
...
NSWindow* theSheet = [self window];
[NSApp endSheet:theSheet returnCode: NSOKButton];
[theSheet orderOut:nil];
最佳答案
有时您需要按 Enter 键来“确认对 cocoa 绑定(bind)的更改”。我不确定,但当您按 Enter 键时,更改和按钮操作可能都会执行。
如果是这种情况,请选择您的 NSTextField 并标记“连续更新值”选项,以便正确同步。
关于cocoa - 单击鼠标时“确定”按钮的行为有所不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5520366/