tags:

views:

59

answers:

1

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?

+4  A: 

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?

Yuji
If setString: doesn't exist then how can I clear the textView?
Phenom
Sorry, `setString:` exists :p I got confused with `-setStringValue:` of `NSControl`.
Yuji
Phenom: You still haven't answered Yuji's question. Also, in your `NSLog` statement, you should log the `textView` variable to see what pointer is there. See the documentation for `NSLog` for details.
Peter Hosey
In interface builder I used NSTextView in an NSScrollView.
Phenom
I deleted the object in interface builder and redid it and now it works for some reason.
Phenom