Question on delegates and multiple text fields

Discussion in 'iOS Programming' started by thedon1, Dec 21, 2012.

  1. thedon1 macrumors 6502

    Joined:
    Jun 26, 2010
    #1
    I'm making an app and trying to learn about Xcode and objective C but just need some help with understanding something.

    I'm looking at examples and uses of delegates, particularly in respect to UITextfields (seems like a good place to start).

    I've found a delegate function textFieldDidEndEditing: and learnt how to use it. If I have a text field and want to do something to the number (e.g. have a % sign next to it), i put that code in the block with the delegate function (in my .m file), change the delegate of the text field the file owner and it all works perfectly.

    What if I have a second text field and I want to use textFieldDidBeginEditing: delegate function and I want it to return say an * when I finish editing instead of a %?

    How can I have 2 instances of the same delegate functions but for different textFields? How do I differentiate? Do I have to use 1 instance of textFieldDidBeginEditing: but use an if function within it which determines what to do depending on the textfield?

    Any clarification on this situation would be great, thanks.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    First, you mention both textFieldDidEndEditing: and textFieldDidBeginEditing:. Make sure you are being consistent in how you want to use them or you will get unexpected results.

    No, you can't have two instances of the same delegate function. But, looking at the documentation for textFieldDidEndEditing:, you should see that it is sent a parameter: textField.
    Code:
    - (void)textFieldDidEndEditing:(UITextField *)textField
    You can use that to differentiate which text field you are dealing with and base your conditional upon it.
     
  3. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #3
    as dejo said you could use the textField parameter that you are sent to differentiate. One way you could do this is by using the tag property of the textField.
     
  4. thedon1, Dec 21, 2012
    Last edited: Dec 21, 2012

    thedon1 thread starter macrumors 6502

    Joined:
    Jun 26, 2010
    #4
    Ok, I get what you're saying. Sorry about using End and Begin.

    I'm just still a little stuck in how I would actually write what the two text fields will do within the delegate method.

    I'm using this code currently within an IBAction which runs when the text field has been changed.

    Code:
     NSString *oldTextFieldValue = AField.text;
        
        AField.text = [NSString stringWithFormat:@"%@ %%",oldTextFieldValue];
        
    
    If i've also got B field, how would I write that in the method?

    Thanks guys
     
  5. PhoneyDeveloper, Dec 21, 2012
    Last edited: Dec 22, 2012

    PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Code:
    if (textField == ATextField)
    {
         // do A stuff
    }
    else
    if (textField == BTextField)
    {
         // do B stuff
    }
    This is why delegate callbacks always should have the object sending the message as the first parameter.

    Another way to handle something like this is to have multiple delegates, but for a simple case like this that's probably more complicated.
     
  6. thedon1 thread starter macrumors 6502

    Joined:
    Jun 26, 2010
    #6
    Awesome, I had a feeling it would be a text field (after some googling) but just wanted to make sure.

    Thanks for the help guys.
     
  7. thedon1 thread starter macrumors 6502

    Joined:
    Jun 26, 2010
    #7
    OK, I have another question related to this.

    I got the delegate method working with my text boxes.

    Code:
    - (void)textFieldDidEndEditing:(UITextField *)UItextfield {
        NSString *oldTextFieldValue = UItextfield.text;
        UItextfield.text = [NSString stringWithFormat:@"%@ %%",oldTextFieldValue];
    }
    Could I instead of having that, have the following action

    Code:
    -(IBAction)Calculate:(UITextField *)UITextfield;
    {
     NSString *oldTextFieldValue = UItextfield.text;
        UItextfield.text = [NSString stringWithFormat:@"%@ %%",oldTextFieldValue];
    }
    
    
    And then in the Delegate function, call that action? Something like
    Code:
    - (void)textFieldDidEndEditing:(UITextField *)UItextfield {
    [self Calculate:self]
    }


    I tried that, it doesn't work. I know it'll get me to the same result but I just want to know if it can be done. I think i'm asking can a method (Calculate) be called in another method (textFieldDidEndEditing) and how.

    Thanks
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    Code:
    [self Calculate:self];
    should be

    Code:
    [self Calculate:textField];
    There's nothing really wrong with calling a delegate method like this from your own code but probably a better way is to have your own method that gets called from both places.

    Also, Cocoa naming conventions start variable names and parameter names with a lower case letter. UItextField for a parameter name is terrible. Don't do that.

    See

    https://developer.apple.com/library...eptual/CodingGuidelines/CodingGuidelines.html
     
  9. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #9

Share This Page