ios 如何获得 UITextField 点击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10045095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get UITextField Tap Event?
提问by Azhar
I am trying to show UIAlertView on Tap or Click of UITextField for both IPad and IPhone. I make an IBAction and Attach it with Tap Down event of UITextField.
我正在尝试在 iPad 和 iPhone 的 UITextField 的 Tap 或 Click 上显示 UIAlertView。我制作了一个 IBAction 并使用 UITextField 的 Tap Down 事件附加它。
But its not working correctly, means not always, in case of iphone and not working in-case of iPad
但它不能正常工作,意味着并非总是如此,在 iPhone 的情况下,而不是在 iPad 的情况下工作
- (IBAction) TopuchState
{
//function code
}
please help How could I do this.
请帮助我怎么能做到这一点。
回答by Lucien
As you are already subscribed to be UITextField delegate, implement this method:
由于您已经订阅了 UITextField 委托,请实现此方法:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alert show];
return YES;
}
回答by J Max
Try adding a target for when a particular text field begins editing (UIControlEventEditingDidBegin
):
尝试添加特定文本字段开始编辑时的目标 ( UIControlEventEditingDidBegin
):
[textField1 addTarget:delegate action:@selector(textField1Active:) forControlEvents:UIControlEventEditingDidBegin];
回答by jaiswal Rajan
In Swift 3
在斯威夫特 3
Add a target for a particular text field for the event .editingDidBegin in viewDidLoad method
在 viewDidLoad 方法中为事件 .editingDidBegin 添加特定文本字段的目标
self.textField.addTarget(self, action: #selector(textFieldTouched(_:)), for: UIControlEvents.editingDidBegin)
func textFieldTouched(textField: UITextField) {
//Show AlertView
}
回答by Zalykr
I think it's easier if you set the delegate for the UITextField
and implement the method:
我认为如果您为 设置委托UITextField
并实现该方法会更容易:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
then inside that method you can easily create and show your UIAlertView
.
然后在该方法中,您可以轻松创建并显示您的UIAlertView
.
Take a look at UITextFieldDelegate.
Good luck!
祝你好运!
回答by Shubham Maheshwari
Connect TextField with delegate and Now calling this function!!
将 TextField 与委托连接,现在调用这个函数!!
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textView.text=@" ";
return YES;
}