macos – 如何检测NSTextField何时具有焦点或是否已选择cocoa的内容

macos – 如何检测NSTextField何时具有焦点或是否已选择cocoa的内容,第1张

概述我在NSTableCellView中有一个NSTextField,我想要一个事件通知我,当我的NSTextField有关于禁用几个按钮的焦点时,我发现了这个方法: -(void)controlTextDidBeginEditing:(NSNotification *)obj{ NSTextField *textField = (NSTextField *)[obj object]; 我在NStableCellVIEw中有一个NSTextFIEld,我想要一个事件通知我,当我的NSTextFIEld有关于禁用几个按钮的焦点时,我发现了这个方法:

-(voID)controlTextDIDBeginEditing:(NSNotification *)obj{    NSTextFIEld *textFIEld  = (NSTextFIEld *)[obj object];    if (textFIEld != _nombreDelPaqueteTextFIEld) {        [_nuevaCuentaActivobutton   setEnabled:FALSE];        [_nuevaCuentaPasivobutton   setEnabled:FALSE];        [_nuevaCuentaIngresosbutton setEnabled:FALSE];        [_nuevaCuentaEgresosbutton  setEnabled:FALSE];    }}

但它会在我的文本字段开始编辑时触发,因为这样说,我希望在我关注textFIEld时禁用按钮,而不是在我已经开始键入时

编辑:要根据Joshua Nozzi收到的帮助我的代码,它仍然无法正常工作

MyNSTextFIEld.h

#import <Cocoa/Cocoa.h>@class MyNSTextFIEld;@protocol MyNSTextFIEldDelegate@optional -(BOol)textFIEldDIDResignFirstResponder:(NSTextFIEld *)sender;@optional -(BOol)textFIEldDIDBecomeFirstResponder:(NSTextFIEld *)sender;@end@interface MyNSTextFIEld : NSTextFIEld@property (strong,nonatomic)           ID <MyNSTextFIEldDelegate> cellVIEw;@end

MyNSTextFIEld.m

#import "MyNSTextFIEld.h"@implementation MyNSTextFIEld- (BOol)becomeFirstResponder{    BOol status = [super becomeFirstResponder];    if (status)        [self.cellVIEw textFIEldDIDBecomeFirstResponder:self];    return status;}- (BOol)resignFirstResponder{    BOol status = [super resignFirstResponder];    if (status)        [self.cellVIEw textFIEldDIDResignFirstResponder:self];    return status;}@end

在我的vIEwcontroller EdicionDeCuentasWC.m上

#import "MyNSTextFIEld.h"@interface EdicionDeCuentasWC ()<NStableVIEwDataSource,NStableVIEwDelegate,NSControlTextEditingDelegate,NSPopoverDelegate,MyNSTextFIEldDelegate>@end@implementation EdicionDeCuentasWC#pragma mark MyNSTextFIEldDelegate-(BOol)textFIEldDIDBecomeFirstResponder:(NSTextFIEld *)sender{    NSLog(@"textFIEldDIDBecomeFirstResponder");    return TRUE;}-(BOol)textFIEldDIDResignFirstResponder:(NSTextFIEld *)sender{    NSLog(@"textFIEldDIDResignFirstResponder");    return TRUE;}#pragma mark --@end

在可视化编辑器中说,已经将我的所有NSTextFIElds更改为MyNSTextFIEld类并将委托设置为我的文件所有者(EdicionDeCuentasWC)

解决方法 我想我已经钉了它.我正在尝试子类化NSTextfiled以覆盖becomeFirstResponder()和resignFirstResponder(),但是一旦我单击它,将调用becomeFirstResponder()并在此之后立即调用resignFirstResponder().咦?但搜索字段仍然处于编辑状态,焦点仍在其中.

我发现,当你点击搜索字段时,搜索字段会成为第一响应者一次,但是NSText将在某个时间某个时间准备好,并且焦点将移动到NSText.

我发现当准备好NSText时,它被设置为self.currentEditor().问题是当finallyFirstResponder()的调用时,self.currentEditor()尚未设置.因此,getsFirstResponder()不是检测它的焦点的方法.

另一方面,当焦点移动到NSText时,调用文本字段的resignFirstResponder(),你知道吗? self.currentEditor()已设置.所以,这是告诉它的代表该文本字段集中的时刻.

接下来,如何检测搜索字段何时失去焦点.再次,这是关于NSText.然后你需要监听NSText委托的方法,比如textDIDEndEditing(),并确保你让它的超类来处理方法,看看self.currentEditor()是否无效.如果是这种情况,NSText会失去它的焦点,并告诉文本字段的代表.

我提供了一个代码,实际上是NSSearchFIEld子类做同样的事情.同样的原则也适用于NSTextFIEld.

protocol ZSearchFIEldDelegate: NSTextFIEldDelegate {    func searchFIEldDIDBecomeFirstResponder(textFIEld: ZSearchFIEld)    func searchFIEldDIDResignFirstResponder(textFIEld: ZSearchFIEld)}class ZSearchFIEld: NSSearchFIEld,NSTextDelegate {    var expectingCurrentEditor: Bool = false    // When you clicked on serach fIEld,it will get becomeFirstResponder(),// and preparing NSText and focus will be taken by the NSText.    // Problem is that self.currentEditor() hasn't been ready yet here.    // So we have to wait resignFirstResponder() to get call and make sure    // self.currentEditor() is ready.    overrIDe func becomeFirstResponder() -> Bool {        let status = super.becomeFirstResponder()        if let _ = self.delegate as? ZSearchFIEldDelegate where status == true {            expectingCurrentEditor = true        }        return status    }    // It is pretty strange to detect search fIEld get focused in resignFirstResponder()    // method.  But otherwise,it is hard to tell if self.currentEditor() is available.    // Once self.currentEditor() is there,that means the focus is moved from     // serach feild to NSText. So,tell it's delegate that the search fIEld got focused.    overrIDe func resignFirstResponder() -> Bool {        let status = super.resignFirstResponder()        if let delegate = self.delegate as? ZSearchFIEldDelegate where status == true {            if let _ = self.currentEditor() where expectingCurrentEditor {                delegate.searchFIEldDIDBecomeFirstResponder(self)                // currentEditor.delegate = self            }        }        self.expectingCurrentEditor = false        return status    }    // This method detect whether NSText lost it's focus or not.  Make sure    // self.currentEditor() is nil,then that means the search fIEld lost its focus,// and tell it's delegate that the search fIEld lost its focus.    overrIDe func textDIDEndEditing(notification: NSNotification) {        super.textDIDEndEditing(notification)        if let delegate = self.delegate as? ZSearchFIEldDelegate {            if self.currentEditor() == nil {                delegate.searchFIEldDIDResignFirstResponder(self)            }        }    }}

您需要将NSSerachFIEld更改为ZSearchFIEld,并且您的客户端类必须符合ZSearchFIEldDelegate而不是NSTextFIEldDelegate.这是一个例子.当用户单击搜索字段时,它会扩展它的宽度,当您单击其他位置时,搜索字段会丢失它的焦点并缩小其宽度,方法是更改​​Interface Builder设置的NSLayoutConstraint的值.

class MyVIEwController: NSVIEwController,ZSearchFIEldDelegate {    // [snip]    @IBOutlet weak var searchFIElDWIDthConstraint: NSLayoutConstraint!    func searchFIEldDIDBecomeFirstResponder(textFIEld: ZSearchFIEld) {        self.searchFIElDWIDthConstraint.constant = 300        self.vIEw.layoutSubtreeIfNeeded()    }    func searchFIEldDIDResignFirstResponder(textFIEld: ZSearchFIEld) {        self.searchFIElDWIDthConstraint.constant = 100        self.vIEw.layoutSubtreeIfNeeded()    }}

它可能取决于 *** 作系统的行为,我尝试了El CAPItan 10.11.4,并且它有效.

代码也可以从Gist复制.
https://gist.github.com/codelynx/aa7a41f5fd8069a3cfa2

总结

以上是内存溢出为你收集整理的macos – 如何检测NSTextField何时具有焦点或是否已选择cocoa的内容全部内容,希望文章能够帮你解决macos – 如何检测NSTextField何时具有焦点或是否已选择cocoa的内容所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1004874.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存