10

I am fairly new to iphone programming and here I am facing some issues. Now in my application, I have two textfields and I want to fire an event while second textfield starts editing. now I am using following function

- (void)textFieldDidBeginEditing:(UITextField *)textField

but the thing is the event is being fired when the first textfield starts editing. It does not wait for the second text field. Is there any way I can use this function for the second textfield or may be somehow could know and pass it the value of the active textfield?

I tried writing the name of the textfield instead of (UITextField *)textField in the function but still the same result.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
Vik
  • 488
  • 2
  • 10
  • 19

6 Answers6

26

If I were you , I would set a tag (in Interface Builder) of the second textField to 2, or something similar. Then you can just do this:

-(void)textFieldDidBeginEditing:(UITextField *)textField {     
    if (textField.tag == 2) {
        //this is textfield 2, so call your method here
    }
}

EDIT: Please do this to see if the method is even called:

-(void)textFieldDidBeginEditing:(UITextField *)textField {     
    NSLog(@"The method was called");
}

For Swift 2.2

func textFieldDidBeginEditing(textField: UITextField) {
    if textField.tag == 2 {
        //this is textfield 2, so call your method here
    }
}
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • hi thanks for reply. but can I add tags to UITextfields as I am not using Interface Builder. I am doing the whole thing programmatically. is it possible in that case? – Vik Apr 16 '11 at 00:31
  • @Vik, you can set in code: `button.tag = 2` or `[button setTag:2]` – Black Frog Apr 16 '11 at 00:40
  • Hi, I actually tried setting textfield tag by locationText.tag =2 and then checking condition by if(textField.tag == 2) in textFieldDidBeginEditing: but it didn't work. same thing, the event didn't get fire. – Vik Apr 16 '11 at 00:54
  • 1
    @Vik Put a NSLog in the in an empty `textFieldDidBeginEditing:` method, and see if it even gets called. Something might be wrong with your delegates. – sudo rm -rf Apr 16 '11 at 01:12
  • @sudo rm -rf ,@ blackfrog -its still not working. I am setting the tag using locationText.tag = 2 but when I print that in didBeginEditing, it shows nothing in that tag value so I am not sure but the event is being called when i run it without the tag but ofcourse on wrong textfield. any suggestions? – Vik Apr 18 '11 at 00:02
  • 1
    @Vik: Check my edit and please try it out. Let me know if you get a message in the console. – sudo rm -rf Apr 18 '11 at 00:47
16

That delegate method is gonna get called everytime the editing of ANY text field is started, so it should be you who controls what is done when this happens. I suggest you to do something like:

   -(void)textFieldDidBeginEditing: (UITextField *)textField
   {     
        if (textField == mySecondTextField)
        {
            //Do what you need
        }
        else
        {
            //Do nothing
        }
   }

I hope it helps you!

Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
  • I totally get it and thats what I am trying to do but now that event is not being fired. Is this the right way to check the value of the textfield becuase I don't think textField has anything in the following code. Please check my code and see whats wrong coz now the event is not getting fired at all -(void)textFieldDidBeginEditing:(UITextField *) textField { if(textField == locationText) { //Doing stuff } Thanks – Vik Apr 16 '11 at 00:24
  • Make sure the view controller implements the `UITextFieldDelegate` protocol and ,as AhmadTK said, you told the text field that the view controller is its delegate. Maybe to get things a little clearer, you should read about the subject, protocols, delegation and stuff. – Fran Sevillano Apr 16 '11 at 00:53
  • @Vik Did you finally get it to work? I hope you did. Please accept the answer that helped you the most. Thanks – Fran Sevillano Apr 16 '11 at 19:40
  • no its still not working. I am setting the tag using locationText.tag = 2 but when I print that in didBeginEditing, it shows nothing in that tag value so I am not sure but the event is being called when i run it without the tag but ofcourse on wrong textfield. any suggestions? – Vik Apr 17 '11 at 23:59
  • Better to check by ``[textField isEqual: mySecondTextField]`` – Taku May 18 '16 at 08:21
4

Utilitize the tag property in Interface Builder to identify your view objects in your application at runtime. It will make life a lot easier, especially when you get ready to localize your application for different languages.

Screenshot of the tab property in IB

In your header file for your view controller

#define kUsernameField 100
#define kPasswordField 101
#define kStartButton 300

In the view controller implementation file

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    switch (textField.tag) {
        case kUsernameField:
            // do user name stuff
            break;

        case kPasswordField:
            // do password stuff
            break;

        default:
            NSLog(@"No case statement for %@", [textField description]);
            break;
    }    
}

You will find a lot of tutorial out there that use the title field of UIButton to identify them. For example:

- (IBAction)buttonTouchUp:(id)sender {
    UIButton *button = (UIButton *)sender;

    // don't like
    if ([button.currentTitle isEqualToString:@"Start"] == NSOrderedSame) {
        // because if localize your for other language then you will have
        // include code for those other language
        //     French: Démarrer
        //     Spanish: Inicio
        // blah blah blah
    }

    // better
    if (button.tag == kStartButton) {
        // very simple, no code changes for localization
        // blah blah blah
    }

}

If you are creating the object with code, you can set the tag:

button.tag = kStartButton;
// or
[button setTag:kStartButton];
Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • 1
    I particularly dislike the concept of using the Interface builder to set the tag property because now you have the ids of the elements in two very different places, which might make it very hard to maintain. I advise you to set the tag property within the code in order to avoid inconsistencies. – Fran Sevillano Apr 16 '11 at 00:45
  • What about objects that I don't connect with IBOutlet? I don't connect every UIButton back to the view controller. THat is another reason I like the tag property. – Black Frog Apr 16 '11 at 00:50
  • Hi, I actually tried setting textfield tag by locationText.tag =2 and then checking condition by if(textField.tag == 2) in textFieldDidBeginEditing: but it didn't work. same thing, the event didn't get fire. – Vik Apr 16 '11 at 00:54
  • Yeah I see that it might come in handy, it's just that I don't like having identifiers in two different places just because they could easily get messed up. – Fran Sevillano Apr 16 '11 at 00:56
  • @Vik in code when you create the UITextField you set the delegate property to your view controller? – Black Frog Apr 16 '11 at 01:08
  • whether you set the tag in IB or in code is personal preference (I do everything in code). regardless, tags are the way to go. – XJones Apr 16 '11 at 02:01
  • @xjones -its still not working. I am setting the tag using locationText.tag = 2 but when I print that in didBeginEditing, it shows nothing in that tag value so I am not sure but the event is being called when i run it without the tag but ofcourse on wrong textfield. any suggestions? – Vik Apr 18 '11 at 00:01
  • 1
    @Vik, if you are setting the `tag` property when creating the label and you are setting the label's `delegate` property to your view controller (most likely `self`) then this will work. – XJones Apr 18 '11 at 00:54
  • @xjones - Thanks a lot. It actually worked. I was just doing something stupid. I just set the delegate right and it worked. Thanks – Vik Apr 18 '11 at 02:07
  • This deserves to be the answer to be honest. He put more work into it than I did. :) – sudo rm -rf Apr 18 '11 at 03:25
2

You must declare first UITextFieldDelegate in your controller .h

And set the delegate of your text field. ex. myInput.delegate = self;

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if ([sender isEqual:myInput])
    {
        NSLog(@"test");

    }
}

This works perfectly for me.

khatzie
  • 2,509
  • 4
  • 22
  • 37
1

Have you checked if your second textViews delegate is set to self ? I had the same issue where I had forgotten to set the delegate of other textFields and hence the delegate method was not firing.

userx
  • 1,083
  • 5
  • 18
  • 36
0

Please have a look to my answer in this Question, it's exactly what you're looking for Objective C: what is a "(id) sender"?

Community
  • 1
  • 1
Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
  • @Ahmad, I totally get it and thats what I am trying to do but now that event is not being fired. Is this the right way to check the value of the textfield becuase I don't think textField has anything in the following code. Please check my code and see whats wrong coz now the event is not getting fired at all -(void)textFieldDidBeginEditing:(UITextField *) textField { if(textField == locationText) { //Doing stuff } Thanks – Vik Apr 16 '11 at 00:25
  • the UITextField you're expecting the event to fire is built through the .XIB file or you add it dynamically ? – Ahmad Kayyali Apr 16 '11 at 00:32
  • I am adding it programmatically. I am not using Interface Builder at all. – Vik Apr 16 '11 at 00:34
  • 1
    have you set the Delegate of the UITextField? yourTextField.delegate = self; – Ahmad Kayyali Apr 16 '11 at 00:36
  • your Owner header file have ? – Ahmad Kayyali Apr 16 '11 at 00:52
  • @ahmad - yes it has that but still not working. I am setting the tag using locationText.tag = 2 but when I print that in didBeginEditing, it shows nothing in that tag value so I am not sure but the event is being called when i run it without the tag but ofcourse on wrong textfield. any suggestions? – Vik Apr 18 '11 at 00:02