简体   繁体   中英

How to clear a text field in Cocoa

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.

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 looks like this:

#import "AppController.h"


@implementation AppController

- (IBAction) clearText: sender 
{ 
    [textView setString: @" "]; 


} 
@end

I connected the button to clearText and the textbox to textView.

The program compiles and runs. But when I press the button, nothing happens. Why is that?

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

- (IBAction)someMethod:(id)sender;

2.Then create an IBOutlet for you text field

 IBOutlet NSTextField *textFieldname;

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

Inside the implementation file (.m) IBAction method do

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

This is very basic. I suggest you google for some tutorials. There's plenty out there. Here's something that may help. Chapter 8 describes how to do exactly what you're asking. link text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM