I have a UITextField that has User Interaction Disabled. So if you tap on this text field, nothing happens. Normally to check if a text field was tapped Id try the delegate methods, but I cannot because user interaction is disabled. Is there any way I can check if the text field was tapped/touched? I change another element to hidden = no; when it is tapped so I was wondering if its even possible enabling user interaction.
我有一个UITextField,它禁用了用户交互。如果你点击这个文本框,什么都不会发生。通常,要检查文本字段是否被选中,请尝试委托方法,但我不能这样做,因为用户交互是被禁用的。是否有办法检查文本字段是否被点击/触摸?我将另一个元素改为hidden = no;当它被点击时,我想知道它是否可能支持用户交互。
2 个解决方案
#1
24
Maybe, you can add UITapGestureRecognizer
in the superview, detect if the touch is inside the frame, and then do something.
也许,您可以在父视图中添加UITapGestureRecognizer,检测触摸是否在框架内,然后做一些事情。
Detect touch if it is inside the frame of the super view
- Create
UITapGestureRecognizer
and add that to theUITextField
's super view. - 创建uitapgesturerecnizer并将其添加到UITextField的超视图中。
- Implement the target selector and check if the gesture's state has ended.
- 实现目标选择器并检查手势的状态是否结束。
- Call your method.
- 打电话给你的方法。
Objective-C
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeTapGesture:)];
[self.textField.superview addGestureRecognizer:tapGesture];
- (void) didRecognizeTapGesture:(UITapGestureRecognizer*) gesture {
CGPoint point = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateEnded) {
if (CGRectContainsPoint(self.textField.frame, point)) {
[self doSomething];
}
}
}
Swift 3
func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didRecognizeTapGesture(_:)))
textField.superView?.addGestureRecognizer(tapGesture)
}
private dynamic func didRecognizeTapGesture(_ gesture: UITapGestureRecognizer) {
let point = gesture.location(in: gesture.view)
guard gesture.state == .ended, textField.frame.contains(point) else { return }
//doSomething()
}
#2
37
Best option is to turn on User Interaction and disable edit action using delegate method.
最好的选择是打开用户交互并禁用使用委托方法的编辑操作。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
You can call your method inside that function to detect tap.
您可以在该函数中调用您的方法来检测tap。
#1
24
Maybe, you can add UITapGestureRecognizer
in the superview, detect if the touch is inside the frame, and then do something.
也许,您可以在父视图中添加UITapGestureRecognizer,检测触摸是否在框架内,然后做一些事情。
Detect touch if it is inside the frame of the super view
- Create
UITapGestureRecognizer
and add that to theUITextField
's super view. - 创建uitapgesturerecnizer并将其添加到UITextField的超视图中。
- Implement the target selector and check if the gesture's state has ended.
- 实现目标选择器并检查手势的状态是否结束。
- Call your method.
- 打电话给你的方法。
Objective-C
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeTapGesture:)];
[self.textField.superview addGestureRecognizer:tapGesture];
- (void) didRecognizeTapGesture:(UITapGestureRecognizer*) gesture {
CGPoint point = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateEnded) {
if (CGRectContainsPoint(self.textField.frame, point)) {
[self doSomething];
}
}
}
Swift 3
func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didRecognizeTapGesture(_:)))
textField.superView?.addGestureRecognizer(tapGesture)
}
private dynamic func didRecognizeTapGesture(_ gesture: UITapGestureRecognizer) {
let point = gesture.location(in: gesture.view)
guard gesture.state == .ended, textField.frame.contains(point) else { return }
//doSomething()
}
#2
37
Best option is to turn on User Interaction and disable edit action using delegate method.
最好的选择是打开用户交互并禁用使用委托方法的编辑操作。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
You can call your method inside that function to detect tap.
您可以在该函数中调用您的方法来检测tap。