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
精彩评论