如何清除可可中的文本字段 [英] How to clear a text field in Cocoa

查看:66
本文介绍了如何清除可可中的文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用最新版本的XCode制作一个简单的Cocoa应用程序。在界面构建器中,我添加了NSTextField和NSButton。当我按下按钮时,我希望它清除文本字段中的所有内容。

I'm trying to make a simple Cocoa application using the newest version of XCode. 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 and runs. But when I press the button, nothing happens. Why is that?

推荐答案

这是失败的原因。
1.在appController类标题中创建一个IBAction。

here's the run down. 1.Create an IBAction in your appController class header.

- (IBAction)someMethod:(id)sender;

2。然后为您创建一个IBOutlet文本字段

2.Then create an IBOutlet for you text field

 IBOutlet NSTextField *textFieldname;

然后将IBAction连接到界面生成器中的按钮,并将IBOutlet连接到文本字段。

You then connect the IBAction to the button in interface builder, and your IBOutlet to your textfield.

在实现文件(.m)中,IBAction方法执行

Inside the implementation file (.m) IBAction method do

- (IBAction)someMethod:(id)sender{
 textFieldname.stringValue = @"";
}

这篇关于如何清除可可中的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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