NSTextField, Help!

Discussion in 'Mac Programming' started by Richard Birkett, Dec 15, 2011.

  1. macrumors member

    Joined:
    Aug 21, 2011
    #1
    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:

    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
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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?
    Code:
    - (void)controlTextDidChange:(NSNotification *)aNotification
    {
    	[COLOR=blue]NSLog(@"%s: aNotification=%@", __PRETTY_FUNCTION__, aNotification);[/COLOR]
    [COLOR=blue]/*[/COLOR]
    	NSString *theString = [_textField stringValue];
    	[RBMExpression expressionWithString:theString];
    [COLOR=blue]*/[/COLOR]
    }
    
     
  3. thread starter macrumors member

    Joined:
    Aug 21, 2011
    #3
    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
     

Share This Page