开发者

textFieldDidBeginEditing: for more than one textfield

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.


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
    }
}


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!


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.

textFieldDidBeginEditing: for more than one textfield

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];


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.


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.


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"?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜