热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在NSTextField中按下Enter键时执行操作?-ExecuteanActionwhentheEnter-KeyispressedinaNSTextField?

Ihaveasmallproblemrightnow.IwanttoexecuteamethodwhentheEnterkeyispressedinaNSTe

I have a small problem right now. I want to execute a method when the Enter key is pressed in a NSTextField. The user should be able to enter his data and a calculation method should be executed as soon as he hits the enter key.

我现在有一个小问题。我想在NSTextField中按下Enter键时执行一个方法。用户应该能够输入他的数据,并且一旦他按下回车键就应该执行计算方法。

8 个解决方案

#1


You can do this by setting the text field's action. In IB, wire the text field's selector to your controller or whatever object presents the IBAction you want to use.

您可以通过设置文本字段的操作来完成此操作。在IB中,将文本字段的选择器连接到控制器或任何呈现您要使用的IBAction的对象。

To set it in code, send the NSTextField a setTarget: message and a setAction: message. For example, if you're setting this on your controller object in code, and your textField outlet is called myTextField:

要在代码中设置它,请向NSTextField发送setTarget:消息和setAction:消息。例如,如果您在代码中的控制器对象上设置此项,并且textField出口名为myTextField:

- (void)someAction:(id)sender
{
  // do something interesting when the user hits  in the text field
}

// ...

[myTextField setTarget:self];
[myTextField setAction:@selector(someAction:)];

#2


You have to do only this

你必须这样做

For some keys (Enter, Delete, Backspace, etc)

对于某些键(Enter,Delete,Backspace等)

self.textfield.delegate = self;

and then implement this method

然后实现此方法

- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{
    NSLog(@"Selector method is (%@)", NSStringFromSelector( commandSelector ) );
    if (commandSelector == @selector(insertNewline:)) {
        //Do something against ENTER key

    } else if (commandSelector == @selector(deleteForward:)) {
        //Do something against DELETE key

    } else if (commandSelector == @selector(deleteBackward:)) {
        //Do something against BACKSPACE key

    } else if (commandSelector == @selector(insertTab:)) {
        //Do something against TAB key
    }

    // return YES if the action was handled; otherwise NO
} 

#3


In your delegate (NSTextFieldDelegate), add the following:

在您的委托(NSTextFieldDelegate)中,添加以下内容:

-(void)controlTextDidEndEditing:(NSNotification *)notification
{
    // See if it was due to a return
    if ( [[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement )
    {
        NSLog(@"Return was pressed!");
    }
}

#4


It's very easy and you can do it directly from UI editor

它非常简单,您可以直接从UI编辑器进行操作

  1. Right click the Text Feild, drag Action reference to the button as shown below in the screenshot
  2. 右键单击Text Feild,将Action引用拖动到按钮,如下面的屏幕截图所示

enter image description here

  1. Now it will give you some option as shown in screen shot below, you need to select perform click
  2. 现在它将为您提供一些选项,如下面的屏幕截图所示,您需要选择执行单击

enter image description here

  1. Now it should look like this
  2. 现在看起来应该是这样的

enter image description here

Note: The event will be raised as soon as you press Tab or Enter key. In case you want the action should only be raised when user presses the Enter key then you have to do a setting. Go to the Attribute inspector and change the Action property to Send on Enter only as shown in screen shot below

注意:只要按Tab键或Enter键,就会引发事件。如果您想要仅在用户按下Enter键时引发操作,则必须进行设置。转到“属性”检查器,将“操作”属性更改为“仅在输入时发送”,如下面的屏幕截图所示

enter image description here

#5


The Swift 3 version of @M.ShuaibImran's solution:

Swift 3版@ M.ShuaibImran的解决方案:

First subclass your ViewController to: NSTextFieldDelegate

首先将ViewController子类化为:NSTextFieldDelegate

class MainViewController: NSViewController, NSTextFieldDelegate {
  ...
} 

Assign the textField delegate to the ViewController in your viewDidLoad():

将textField委托分配给viewDidLoad()中的ViewController:

self.textField.delegate = self

Include the NSTextFieldDelegate method that handles keyboard responders:

包括处理键盘响应器的NSTextFieldDelegate方法:

func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
    if (commandSelector == #selector(NSResponder.insertNewline(_:))) {
        // Do something against ENTER key
        print("enter")
        return true
    } else if (commandSelector == #selector(NSResponder.deleteForward(_:))) {
        // Do something against DELETE key
        return true
    } else if (commandSelector == #selector(NSResponder.deleteBackward(_:))) {
        // Do something against BACKSPACE key
        return true
    } else if (commandSelector == #selector(NSResponder.insertTab(_:))) {
        // Do something against TAB key
        return true
    } else if (commandSelector == #selector(NSResponder.cancelOperation(_:))) {
        // Do something against ESCAPE key
        return true
    }

    // return true if the action was handled; otherwise false
    return false
}

#6


NSTextFieldDelegate's – control:textView:doCommandBySelector: is your friend.

NSTextFieldDelegate的 - 控件:textView:doCommandBySelector:是你的朋友。

Look for the insertNewline: command.

查找insertNewline:命令。

#7


In Interface Builder - click on your NSTextField, go to the connections editor, drag from selector to your controller object - you're actions should come up!

在Interface Builder中 - 单击您的NSTextField,转到连接编辑器,从选择器拖动到您的控制器对象 - 您应该采取行动!

#8


Best way to do that is to bind the NSTextField value with NSString property.

最好的方法是将NSTextField值与NSString属性绑定。

For Example,define a method:

例如,定义一个方法:

(void)setTextFieldString:(NSString *)addressString {}

add bindings:

[textField bind:@"value" toObject:self withKeyPath:@"self.textFieldString" options:nil];

Enter any text and hit the return key, setTextFieldString will be called.

输入任何文本并点击返回键,将调用setTextFieldString。


推荐阅读
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 如何实现JDK版本的切换功能,解决开发环境冲突问题
    本文介绍了在开发过程中遇到JDK版本冲突的情况,以及如何通过修改环境变量实现JDK版本的切换功能,解决开发环境冲突的问题。通过合理的切换环境,可以更好地进行项目开发。同时,提醒读者注意不仅限于1.7和1.8版本的转换,还要适应不同项目和个人开发习惯的需求。 ... [详细]
author-avatar
hjalshj
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有