PDA

View Full Version : NSTextField, Help!




Richard Birkett
Dec 15, 2011, 03:52 PM
I'm not new to this, but I've taken a break from programming. I say that because it sounds stupid...

My textfield is sending a notification twice! Worse than that, it types the letter twice! Without anything set up it types and acts normally, plain old text field. If I set the appDelegate to the text field's delegate using IB and then use the following code:

#import <Cocoa/Cocoa.h>
#import "RBMExpression.h"

@interface RBMAppDelegate : NSObject <NSApplicationDelegate>

- (void)controlTextDidChange:(NSNotification *)aNotification;

@property (strong) IBOutlet NSTextField *myTextField;
@property (assign) IBOutlet NSWindow *window;

@end


#import "RBMAppDelegate.h"

@implementation RBMAppDelegate

@synthesize myTextField = _textField;
@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}

- (void)controlTextDidChange:(NSNotification *)aNotification
{
NSString *theString = [_textField stringValue];
[RBMExpression expressionWithString:theString];
}
@end


It starts double typing each character and sending the notification twice.

Any help appreciated!!!

Richard



jiminaus
Dec 15, 2011, 06:46 PM
If you comment out the body of your controlTextDidChange: but still leave the delegate connection in place in IB, do you still get this behaviour?

- (void)controlTextDidChange:(NSNotification *)aNotification
{
NSLog(@"%s: aNotification=%@", __PRETTY_FUNCTION__, aNotification);
/*
NSString *theString = [_textField stringValue];
[RBMExpression expressionWithString:theString];
*/
}

Richard Birkett
Dec 15, 2011, 07:27 PM
Hi,

Sorry, panic over, thanks for replying. It was strange to be fair, it was due to an error in expressionWithString: from trying to get an out of bounds substring of an NSString (using a bad for loop and substringWithRange:), I still don't understand fully but it was logging an error amongst my other logs, and for some unknown reason it had to repeat the notification! Fixing the small error fixed this problem.

Thanks,

Richard