10 Replies Latest reply: Oct 1, 2015 8:58 AM by red_menace
Apple_For_The_Win Level 1 Level 1

I'm working on an application where I have one text field, and below it, a "Send" button. When the button is pressed, is sends whatever is typed in the field to all the user's Skype contacts. I have to prevent the user from sending a blank message to all contacts, so I have to change the button's setEnabled. I've been using an NSTimer to check every second, which works, but it lags my application hugely. Is there any handler that is run whenever text is added or deleted? Perhaps something like:

 

on myTextFieldDidChange_(aNotification)
Solved by red_menace on Sep 19, 2013 8:06 AM Solved

A specific text field is not used in the handler call - the delegate method is called for all text fields.  In the case where you have several text fields, you can get the notification's object, for example:

 

on controlTextDidChange_(aNotification) -- a text field changed, so check it out

  set thisTextField to aNotification's object() -- the current control being changed

  set theText to thisTextField's stringValue()

  log theText

  -- whatever

end controlTextDidChange_


Note that you need to connect the text field's delegate outlet to your AppDelegate (or whatever class you are using for the delegate).

Reply by Frank Caggiano on Sep 19, 2013 4:54 AM Helpful

Why not just check the text field in the method that is called when the send button is pressed and if the field is blank just ignore it?

Reply by red_menace on Sep 19, 2013 7:46 AM Helpful

You are looking for NSControl's controlTextDidEndEditiing: delegate method.

All replies

  • Frank Caggiano Level 7 Level 7

    Why not just check the text field in the method that is called when the send button is pressed and if the field is blank just ignore it?

  • Apple_For_The_Win Level 1 Level 1

    Because afterwards I plan to add other things, like emoticon and code recognition during typing.

  • Frank Caggiano Level 7 Level 7

    So your looking to be called each time a single char is typed in a text field?

  • Apple_For_The_Win Level 1 Level 1

    Yes.

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    You are looking for NSControl's controlTextDidEndEditiing: delegate method.

  • Apple_For_The_Win Level 1 Level 1

    So if my text field is called "mytextfield" then what would I put for the handler?

  • Frank Caggiano Level 7 Level 7

    red_menace answered your actual question  however in your first post this is what you wrote:

     

    I have to prevent the user from sending a blank message to all contacts, so I have to change the button's setEnabled.

     

    preventing a blank message from going out can be done by checking the text field once the users presses return or the send button and is a lot more efficient then checking for each typed char.

     

    You'll get better responses to your questions and save everyone's time if you can actually ask the question that you want answered and supply all the relevant information up-front.

     

     

    And could you do us all a favor and trim the tags  you add to tour posts. Makes it a pain when reading here on a mobile device.

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    A specific text field is not used in the handler call - the delegate method is called for all text fields.  In the case where you have several text fields, you can get the notification's object, for example:

     

    on controlTextDidChange_(aNotification) -- a text field changed, so check it out

      set thisTextField to aNotification's object() -- the current control being changed

      set theText to thisTextField's stringValue()

      log theText

      -- whatever

    end controlTextDidChange_


    Note that you need to connect the text field's delegate outlet to your AppDelegate (or whatever class you are using for the delegate).

  • ManManInfo Level 1 Level 1

    Help I got an error:

     

    -[NSTextField object]: unrecognised selector sent to instance 0x6080001816c0

     

    Please Help!

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    You should start a new topic with your particular problem, since the specific details in your situation would be different and your reply doesn't add to solving the OP's (solved) problem.

     

    With that said, the "unrecognised selector" error message usually means that the message (handler call) sent to an object can't be found, which could be a result of misspelling (including capitalization, since Cocoa methods are case sensitive) or a wrong or missing object (e.g. trying to use a textField method on a button, or trying to use a method on an object that hasn't been created).