我想问一下如何在iphone UITextField中单击(触摸开始写入)来调用方法,就像我们单击UIButton并将方法放在“addTarget”中一样,对于UITextFields有什么办法吗?
implement a UITextFieldDelegate and do whatever you want to do in - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
or - (void)textFieldDidBeginEditing:(UITextField *)textField
You should use the first method if you want to stop the textfield from behaving like a textfield. For example if you want to open the textfield editor in a modal view. You can return NO there if you don't want this behavior.
Edit: Here is the code to call myMethod:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self myMethod];
return YES;
}
You can set textField's delegate and implement textFieldShouldBeginEditing:
method in it - that method will get called when user taps the field and before it goes into editing.
See UITextFieldDelegate
reference for more methods available.
[textField addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventEditingChanged];
-(void) myMethod:(id)sender
{
UITextField* textField = (UITextField *)sender;
}
i think this will help..!!
You can code as,
[YourTextField addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventEditingDidBegin];
-(void)myMethod
{
[YourTextField resignFirstResponder];
//Do whatever you want
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.