|
|
#1 |
|
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. |
|
|
|
0
|
|
|
#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.
Quote:
Code:
- (void)textFieldDidEndEditing:(UITextField *)textField
__________________
|
||
|
|
0
|
|
|
#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.
__________________
2012 Mac Mini, 2.6 GHz, 16GB RAM, 1TB HDD ![]() 2.4Ghz 15" Macbook Pro ![]() 32 GB iPhone 4S : 16 GB iPod nano : 16 GB iPad 3 Nikon D60 : 18-55 mm VR : 55-200 mm VR |
|
|
|
0
|
|
|
#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];
Thanks guys Last edited by thedon1; Dec 21, 2012 at 04:56 PM. |
|
|
|
0
|
|
|
#5 |
|
Code:
if (textField == ATextField)
{
// do A stuff
}
else
if (textField == BTextField)
{
// do B stuff
}
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. |
|
|
|
0
|
|
|
#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. |
|
|
|
0
|
|
|
#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];
}
Code:
-(IBAction)Calculate:(UITextField *)UITextfield;
{
NSString *oldTextFieldValue = UItextfield.text;
UItextfield.text = [NSString stringWithFormat:@"%@ %%",oldTextFieldValue];
}
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 |
|
|
|
0
|
|
|
#8 |
|
Code:
[self Calculate:self]; Code:
[self Calculate:textField]; 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 |
|
|
|
0
|
|
|
#9 | |
|
Quote:
|
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 11:16 PM.







I support the 
2012 Mac Mini, 2.6 GHz, 16GB RAM, 1TB HDD
Linear Mode
