Multiple IBAction's?

Discussion in 'Mac Programming' started by Fritzables, Jun 24, 2011.

  1. macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #1
    Quick question.....

    Can a single control have multiple IBActions ??

    Pete
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    No. At least not at the same time. What are you trying to achieve?
     
  3. thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #3
    G'Day Jim,

    How's it go'n down there??

    OK.... I have a TextField that I will need to evaluate every key entry as the end user types in a value - is this just a normal IBAction ??

    Pete
    Brisbane
     
  4. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    G'day Pete,

    I think you're mistaken about which way's up. :p

    An action is not what you want if you want to react to each change.

    If you want to react whenever the text field is change (be it by pressing a key, cutting/pasting text, etc.) then what you want to do is set up a delegate to the text field.

    Connect the delegate outlet to whatever the nearest window/view controller. Then in the controller implement the following method.

    Code:
    - (void)controlTextDidChange:(NSNotification *)aNotification
    {
        NSTextField* textField = [aNotification object];
        NSString* newValue = [textField stringValue];
        
        NSLog(@"%s: %@", __PRETTY_FUNCTION__, newValue);
        // TODO: React to changed text field
    }
    
    Jim
     
  5. thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #5
    Thanks once again Jim for the guidance on this one. Much appreciated.

    Go the Maroons :D


    Pete
     
  6. thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #6
  7. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    Hi Pete,

    It seems to work.

    I wouldn't use intValue/setIntValue. I would instead use integerValue/setIntegerValue so 64-bit numbers can be entered in 64-bit builds.

    I also wouldn't call the delegate explicitly. The blog posters code is wrong because it doesn't check that the delegate actually responds to controlTextDidChange:. Because NSTextField's textDidChange: sends controlTextDidChange: to the delegate if appropriate, I would just let that code still do its job by call super.

    So I would code the textDidChange:.
    Code:
    -(void)textDidChange:(NSNotification *)aNotification
    {
        [self setIntegerValue:[self integerValue]];
        [super textDidChange:aNotification];
    }
    
    Another way would be to do Windows-style filtering of keyDown events. But then you'd also need to handle other ways of getting text into the text field, such as copy and paste.

    Like the blog poster, I don't like the UX of trapping a user into a text field. Personally, I don't try to do input validation at the control level. I do it at the form level when the form is "submitted".

    Jim
     
  8. thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #8
    Afternoon Jim,

    Settled with your code - works great - all I will need to do is convert the newValue to a float.

    One thing I am failing to understand and hope you may steer me in the right direction.

    In your code there is no reference to the TextField pointer I am using yet your code responds to my user input - how does this happen if I have made no reference in you code?

    Pete
     
  9. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #9
    NSTextField inherits from NSControl. NSControl has getter and setters for the control's value as different types. See Setting the Control's Value.

    Assuming you're talking about textDidChange:, self is the pointer to the NSTextField-subclass. "self" is equivalent to "this" in other languages such as C# and Java.

    If you're talking about controlTextDidChange:, the NSTextField(-subclass) can be retrieved by [aNotification object].
     
  10. thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #10
    So, what you're saying is, as NSTextField inherits the NSText object which has the textDidChange delegate which will post a notification when an end user taps a key.

    Then the method you wrote will handle all messages associated with the Notification?

    What would happen if the interface has a number of TextFields within, I guess the coder then will need to reference using 'id' ?

    Thanks again Jim, sorry to bother you.

    Pete
     
  11. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #11
    NSTextField doesn't inherit from NSText. It does have the same delegate methods though.

    I'm still not sure which method you're actually referring? The textDidChange: method in the NSTextField subclass, or the controlTextDidChange: method in delegate.
     

Share This Page