此可可代码有什么问题? [英] What's wrong with this Cocoa code?
问题描述
我正在尝试使用XCode 3.2.3制作一个简单的Cocoa应用程序。在界面构建器中,我添加了NSTextField和NSButton。当我按下按钮时,我希望它清除文本字段中的所有内容。
I'm trying to make a simple Cocoa application using XCode 3.2.3. In interface builder I added NSTextField and NSButton. When I press the button, I want it to clear whatever is in the text field.
我创建了一个名为AppController.h的新类。这是内容:
I made a new class called AppController.h. This is the contents:
#import <Foundation/Foundation.h>
@interface AppController : NSObject {
IBOutlet id textView;
}
- (IBAction) clearText: sender;
@end
AppController.m看起来像这样:
AppController.m looks like this:
#import "AppController.h"
@implementation AppController
- (IBAction) clearText: sender
{
[textView setString: @" "];
}
@end
我已连接
程序可以正确编译并运行。但是当我按下按钮时,什么也没发生。为什么?
The program compiles without error and runs. But when I press the button, nothing happens. Why is that?
推荐答案
使用 id
来获取 IBOutlet
是一个不好的做法。使用
Using id
for an IBOutlet
is a bad practice. Use
IBOutlet NSTextView* textView;
。
请使用调试器进行检查,或在<$ c $之前放置 NSLog(@ foo!);
c> [textView setString:@] 查看该操作方法是否真正被调用。
Please check using the debugger, or putting NSLog(@"foo!");
before [textView setString:@""]
to see if the action method is really called.
另一个陷阱是存在 NSTextView
和 NSTextField
。这两个是不同的!
前者同时支持 setString:
和 setStringValue:
,而后者仅支持 setStringValue:
。
您在界面生成器中使用了哪个对象?
Another pitfall is that there are NSTextView
and NSTextField
. These two are different!
The former supports both setString:
and setStringValue:
, while the latter only supports setStringValue:
.
Which object did you use in the interface builder?
这篇关于此可可代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!