Assign a method for didEndOnExit event of UITextField

纵然是瞬间 提交于 2019-11-30 04:48:12

问题


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

This is easy enough to do in IB but I can't figure out how to do it in code.


回答1:


I just figured it out...

[mytextField addTarget:self 
        action:@selector(methodToFire:)
        forControlEvents:UIControlEventEditingDidEndOnExit];



回答2:


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;



回答3:


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


来源:https://stackoverflow.com/questions/861550/assign-a-method-for-didendonexit-event-of-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!