开发者

How to clear a text field in Cocoa

开发者 https://www.devze.com 2023-01-06 13:58 出处:网络
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

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 setSt开发者_如何转开发ring: @" "]; 


} 
@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

0

精彩评论

暂无评论...
验证码 换一张
取 消