Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thedon1

macrumors 6502a
Original poster
Jun 26, 2010
529
73
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.

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?

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.
 

waterskier2007

macrumors 68000
Jun 19, 2007
1,871
228
Novi, MI
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.
 

thedon1

macrumors 6502a
Original poster
Jun 26, 2010
529
73
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
 
Last edited:

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 
Last edited:

thedon1

macrumors 6502a
Original poster
Jun 26, 2010
529
73
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.
 

thedon1

macrumors 6502a
Original poster
Jun 26, 2010
529
73
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
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.