0
votes

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.

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 without error and runs. But when I press the button, nothing happens. Why is that?

1
Did you step through the code to confirm that clearText is being called? - Joost Schuur
You've already asked the same question here: stackoverflow.com/questions/3182304/… - Joost Schuur
Phenom: Setting the string value to a space isn't really clearing it, as it will not be empty. It will contain one character, which is a space (U+0020). - Peter Hosey
Phenom: Also, Xcode is an IDE. Asking about “this Xcode code” makes no more sense than “this vim code” or “this emacs code”. The only way it would be accurate would be if you were asking about the source code to Xcode itself. See my edit to your title and tags for the correct wording. - Peter Hosey
I don't really care if the resulting string contains one space. I just want to clear the window of any text that has been entered. - neuromancer

1 Answers

4
votes

Using id for an IBOutlet is a bad practice. Use

      IBOutlet NSTextView* textView;

instead.

Please check using the debugger, or putting NSLog(@"foo!"); before [textView setString:@""] to see if the action method is really called.

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?