NSTextField委托通知 - 如何获取文本? [英] NSTextField delegate notifications -- how to get text?

查看:676
本文介绍了NSTextField委托通知 - 如何获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力学习使用Xcode,但我很困惑如何注册 NSTextField 已更改。基本上,我有一个 NSTextField 和一个按钮。点击按钮对字段中的文本进行填充。但是,我想要能够获得字段的文本,而不需要使用文本字段操作:发送结束编辑。也就是说,我想要能够输入文本,并立即按下按钮,而不用敲击或者跳出文本框。看起来像这样做的方法是为我的 NSTextField 设置一个委托,响应

I've been trying to learn to use Xcode, but I'm getting confused with how to register that NSTextField has changed. Essentially, I have an NSTextField and a button. Clicking the button does stuff with the text in the field. However, I want to be able to get the text of the field without needing to use the text field "Action:send on end editing." That is, I want to be able to enter text and immediately press the button, without hitting enter or tabbing out of the text box. It seems like the way to do this would be by setting a delegate for my NSTextField that responds to

- (void)controlTextDidChange:(NSNotification *)aNotification

不知道如何获取已输入的文本。我假设它与

But I don't understand how to get the text that has been entered. I assume it has something to do with

[[aNotification userInfo] valueForKey:@"NSFieldEditor"];

但我真的不知道从那里去。

but I really have no idea where to go from there.

推荐答案

你在正确的轨道上!您从通知的用户信息字典中得到的对象是字段编辑器,这只是一个 NSTextView ,它代表文本字段处理文本输入。

You're on the right track! The object that you get out of the notification's user info dictionary is the Field Editor, which is simply an NSTextView that's handling the text input on the text field's behalf.

一次你有那个对象,你所要做的就是要求它的 textStorage ,这是一个 NSTextStorage *保存文本的对象。那个对象又有 string ,它是一个只保存字符的普通的 NSString

Once you have that object, all you have to do is ask it for its textStorage, which is an NSTextStorage* object holding the text. That object, in turn, has its string which is a plain old NSString holding just the characters.

NSTextView * fieldEditor = [[aNotification userInfo] objectForKey:@"NSFieldEditor"];
NSString * theString = [[fieldEditor textStorage] string];






* NSAttributedString ,它是一个包含字符串和相关联的属性(如颜色,字体和下划线)的对象。


*A subclass of NSAttributedString, which is an object holding a string and associated "attributes" like color, font, and underlining.

这篇关于NSTextField委托通知 - 如何获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆