对NSTextField进行子类化 [英] Subclassing a NSTextField

查看:120
本文介绍了对NSTextField进行子类化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先;我是一个完整的新手可可,所以请裸露我,问我什么,我相信是一个无尽的和全新的新问题。

First off; I'm a COMPLETE newbie at Cocoa, so please bare with me for asking what I am sure is an inane and utter newbish question.

我的情况如下:我想扩展NSTextField的功能。 AMong我想实现的事情是:

My situation is as follows: I want to expand on the functionality of NSTextField. AMong the things I want to achieve is:

更改插入符的外观和感觉。

Changing the look and feel of the caret.

检测文本达到一定数量的字符,然后在该限制后不同地着色文本。 *

Detecting when the text reaches a certain number of characters and then coloring the text after that limit differently. *

对于我花了相当一段时间后我的极大的沮丧,我发现几百个命中,简单地说Sublass NSTextField和使用这段代码,和我的耻辱发现自己无法知道如何做到这一点。

To my great frustration after spending quite some time googling I find hundreds of hits that simply state "Sublass NSTextField and use this code.", and to my humiliation I have found myself unable to grok exactly how to do this.

如果有人可以给我一个子类的工作示例,实现上面列出的两个内容之一,以及如何实现

I would be extremely grateful if someone could give me a working example of a subclass that achieves one of the two things I list above, and instructions* on how to implement the code so I can try and figure out how it works by looking at some actual live code.

对所有人表示歉意。我在家里有一个腹痛婴儿,因为你(或至少那些有孩子的人)可以想象这会占用你很多可用的时间。感谢大家的回应。

Apologies to all of you. I have a colic infant at home, and as you (or at least those of you that have children) can imagine this takes up quite a lot of your available time. Thank you all for your responses.

我看到我的主要问题之一是我对代表和网点缺乏足够的了解。我购买了这里推荐的书(以及许多其他地方,我收集了一些圣经),我正在研究它,因为我们在这几天沉默的时候说话。 :)

I see that one of my main problems is that I don't have a sufficient understanding of delegates and outlets. I have purchased the book recommended here (and many other places. Some sort of "Bible" I gather) and I'm looking into it as we speak in the few silent hours I have these days. :)

但是,虽然我可以看到它对我来说是一个不可或缺的工具,但我仍然通过研究示例而不是阅读理论获得最多的理解*,所以我会非常感谢,如果有人会创建一个相关类的正确的子类的项目,因为我明白我可能不应该扩展NSTextfield类?

But although I can see it's going to be an indispensable tool for me I still gain the most understanding from studying examples rather than reading the theory* and so I would be extremely grateful if someone would create a project with a proper subclass of the relevant class since I understand that I should probably not be extending the NSTextfield class?

我会立即标记标记Thalmans帖子作为答案,因为我相信这是一个适当的假人的反应,但我会坚持几天,因为我真的很喜欢一个文件,仔细阅读。但我不是忘恩负义!

I would instantly mark Mark Thalmans post as the answer as I'm sure it's a proper "for dummies" response, but I'll hold out for a few days since I'd really love a file to peruse. But I am not ungrateful!

哦,和;请相信我的家伙,当我说我不是无用的语言我实际上知道。这只是这些概念与Interface Builder和GUI连接到代码是非常未知的我。我通常只是写代码,并保持它。

Oh, and; Please believe me guys when I say I'm not quite as useless in languages I actually know. It's just that these concepts with the Interface Builder and GUIs connection to the code is very unknown to me. I usually just write the code and keep it at that.

*是的,我的第一个小培训项目确实是一个Twitter效用。

*Yes, my first little training project is indeed a Twitter Utility.

*喜欢给孩子

*不是说读这个理论没有巨大的价值我也是。我不会是我没有Colin Moock最终指南AS3的地方

*Not that reading the theory hasn't got tremendous value for me as well. I wouldn't be where I am without Colin Moock definitive guide to AS3

推荐答案

Martin,

我从新建文件对话框开始,左边选择了Cocoa,然后选择了Objective-C类。

I started with the "New File" dialog and chose "Cocoa" on the left and then Objective-C class.

下面的代码,没有注释。然后你需要做的是将头的@interface行中的NSObject更改为NSTextView,你有一个工作的子类。如果你使用的XCode 3.0,你可以去Interface Builder,并将你的NSTextField的类更改为MyTextView。

That will generate the following code, without the comments. Then all you need to do is change the NSObject in the"@interface" line of the header to "NSTextView" and you have a working subclass. If you are using XCode 3.0 you can go to Interface Builder and change class of your NSTextField to "MyTextView".

你也应该选择Aaron Hillegass的书Cocoa Programming对于Mac OS X,第三版如果你还没有更新为豹的豹。

You should also pick up Aaron Hillegass' book "Cocoa Programming for Mac OS X, Third Edition" It has bee updated for Leopard, if you haven't already.

祝你好运。


#import <Cocoa/Cocoa.h>


@interface MyTextView : NSTextView {
 // Outlets & Members go here
}
// Actions & messages go here
@end




#import "MyTextView.h"


@implementation MyTextView

@end

这篇关于对NSTextField进行子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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