gpt4 book ai didi

cocoa 通知,如何观察事件?

转载 作者:行者123 更新时间:2023-12-03 17:21:57 27 4
gpt4 key购买 nike

我的 Cocoa 应用程序中有一个 TextField。该文本字段有时会被填充,有时会为空。

我希望当字段为空时按钮被禁用。现在,每当我对 Core Data 执行某些操作时,我都会检查该字段,TextField 从中获取其值。

我希望始终对其进行检查。

最佳答案

如果用户编辑文本字段并打开字段编辑器,那么您可以使用以下内容。

您可以实现NSTextFieldDelegate协议(protocol)(实际上就是 NSControlTextEditing 协议(protocol))。例如,在 Controller 或委托(delegate)对象中实现 control:textShouldEndEditing: 方法并使用它来检查新字符串的值。如果字符串为空,则禁用该按钮。

// In a control object which has a reference to the NSTextField and the NSButton
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor {
if ([fieldEditor.string isEqualToString:@""])
{
[self.button setEnabled:NO];
return YES;
}

[self.button setEnabled:YES];
return YES;
}
<小时/>

更新:抱歉错过了有关 CodeData 的部分。在这种情况下,您应该按照此处的说明进行操作,Disable/Enable NSButton if NSTextfield is empty or not ,

The other way would be to give the controller a property exposing the string value, bind the text field's value binding to this stringValue property, and bind the button's enabled binding to the controller's stringValue.length.

关于 cocoa 通知,如何观察事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21995428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com