单击鼠标时“确定”按钮的行为有所不同

发布于 2024-10-29 11:34:06 字数 972 浏览 2 评论 0原文

我有一个带有 NSTextField 控件、一个“确定”按钮和一个“取消”按钮的模式表。 OK 按钮绑定到我的控制器类中名为 theSheetOK 的操作方法。我还将 NSTextField 控件绑定到控制器(文件所有者)中名为 fooNSString 成员,并使用键值绑定来读取文本用户输入的值(即绑定检查器中文本字段的模型键路径设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2024-11-05 11:34:06

有时您需要按 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.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文