Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 21, 2012, 01:06 PM   #1
thedon1
macrumors 6502
 
Join Date: Jun 2010
Question on delegates and multiple text fields

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.
thedon1 is offline   0 Reply With Quote
Old Dec 21, 2012, 01:34 PM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: 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.

Quote:
Originally Posted by thedon1 View Post
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.
__________________
dejo is offline   0 Reply With Quote
Old Dec 21, 2012, 02:07 PM   #3
waterskier2007
macrumors 68000
 
waterskier2007's Avatar
 
Join Date: Jun 2007
Location: White Lake, MI
Send a message via AIM to waterskier2007
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.
__________________
2012 Mac Mini, 2.6 GHz, 16GB RAM, 1TB HDD
2.4Ghz 15" Macbook Pro
16 GB iPhone 5 : 32 GB iPhone 4S : 16 GB iPad 3
16 GB Nexus 7 (2012)
waterskier2007 is online now   0 Reply With Quote
Old Dec 21, 2012, 04:48 PM   #4
thedon1
Thread Starter
macrumors 6502
 
Join Date: Jun 2010
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 by thedon1; Dec 21, 2012 at 04:56 PM.
thedon1 is offline   0 Reply With Quote
Old Dec 21, 2012, 11:56 PM   #5
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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 by PhoneyDeveloper; Dec 22, 2012 at 10:12 AM.
PhoneyDeveloper is offline   0 Reply With Quote
Old Dec 22, 2012, 04:41 AM   #6
thedon1
Thread Starter
macrumors 6502
 
Join Date: Jun 2010
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 is offline   0 Reply With Quote
Old Dec 22, 2012, 12:30 PM   #7
thedon1
Thread Starter
macrumors 6502
 
Join Date: Jun 2010
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
thedon1 is offline   0 Reply With Quote
Old Dec 22, 2012, 01:09 PM   #8
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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/...uidelines.html
PhoneyDeveloper is offline   0 Reply With Quote
Old Dec 22, 2012, 11:43 PM   #9
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by PhoneyDeveloper View Post
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/...uidelines.html
On a similar note, shouldn't his method Calculate: be calculate:... Or better yet, something more descriptive like calculateFromTextIn:?
ArtOfWarfare is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Multiple text messsages sending with 7.0.5 sheareb iOS 7 7 Feb 1, 2014 07:15 PM
How do I make it so editing text fields updates display as I type? printz Mac Programming 2 May 5, 2013 12:45 AM
Another multiple ID, multiple device, multiple phone number iMessage question mrcalabash iOS 6 3 Jan 12, 2013 07:45 PM
getting multiple conversations in text with the same person kiko1 iOS 6 8 Oct 11, 2012 09:15 AM
iPhone: Same text message on multiple devices tcurti iOS 6 1 Oct 4, 2012 10:27 PM

Forum Jump

All times are GMT -5. The time now is 02:16 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC