单击鼠标时“确定”按钮的行为有所不同
我有一个带有 NSTextField 控件、一个“确定”按钮和一个“取消”按钮的模式表。 OK 按钮绑定到我的控制器类中名为 theSheetOK 的操作方法。我还将 NSTextField 控件绑定到控制器(文件所有者)中名为 foo 的 NSString 成员,并使用键值绑定来读取文本用户输入的值(即绑定检查器中文本字段的模型键路径设置为 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];
I have a modal sheet with an NSTextField control, an OK button and a Cancel push button. The OK button is bound to an action method called theSheetOK in my controller class. I also bound NSTextField control to an NSString member named foo in my controller (File's Owner) and I use key-value bindings to read the text value user entered (i.e. model-key path of text field in the bindings inspector is set to foo).
All works fine if the text is entered and user hits the OK button via keyboard. When I trace the value of foo with NSLog in theSheetOK handler I see the value I just typed in the text field.
However when I clicked the OK button with mouse, the value of foo is logged as empty, also as soon as I click the OK button, the text field control grabs the focus and the text I typed appears selected. Any ideas what went wrong?
@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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有时您需要按 Enter 键来“确认对可可绑定的更改”。我不确定,但当您按 Enter 键时,更改和按钮操作可能都会执行。
如果是这种情况,请选择您的 NSTextField 并标记“连续更新值”选项,以便正确同步。
Sometimes you need to press enter to "confirm a change" to cocoa bindings. I'm not sure, but it's possible that when you hit enter both the change and button action are performed.
If that's the case, select your NSTextField and mark the option "Continuously Updates Value" so things get synced properly.