Assign a method for didEndOnExit event of UITextField

后端 未结 3 1004
南旧
南旧 2021-01-05 07:17

How do I programmatically assign a method (observer?) to the didEndOnExit event of a UITextField object?

This is easy enough to do in IB bu

相关标签:
3条回答
  • 2021-01-05 08:01

    I just figured it out...

    [mytextField addTarget:self 
            action:@selector(methodToFire:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
    
    0 讨论(0)
  • 2021-01-05 08:02

    In your view controller implement the following method:

    - (void)textFieldDidEndEditing:(UITextField *)textField{
    
    //do stuff
    
    }
    

    Don't forget to set the delegate in viewDidLoad or the initializer:

    myTextField.delegate = self;
    
    0 讨论(0)
  • 2021-01-05 08:03

    For anyone considering to use this target to make another textfield becomeFirstResponder to create a 'Next' button option that feels like you are 'tabbing' through textFields I would recommend using textFieldShouldReturn instead with code like this. Let me tell you it works so well you'll feel like you're riding a unicorn:

        -(BOOL)textFieldShouldReturn:(UITextField *)textField{
        [textField resignFirstResponder];
    
        if([textField isEqual:_textField1]){
            [_textField2 becomeFirstResponder];
            return NO;
        }else if([textField isEqual:_textField2]){
            [_textField3 becomeFirstResponder];
            return NO;
        }
    
        return YES;
    }
    
    0 讨论(0)
提交回复
热议问题