gpt4 book ai didi

ios - 按下时为 UITextfield 执行操作

转载 作者:行者123 更新时间:2023-11-28 20:10:36 25 4
gpt4 key购买 nike

我有一个文本框,我想在我按下时给它一个 Action 。

我尝试了两件事:1) TouchUpInside 时给它 Action 。

[_countryTF addTarget:self action:@selector(testing:) forControlEvents:UIControlEventTouchUpInside];

-(IBAction)testing:(id)sender
{
NSLog(@"testing");
}

但测试操作未被调用。

2) 我试着用这样的通知来处理它

    [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHideHandler:)
name:UIKeyboardWillShowNotification
object:nil];

- (void) keyboardWillHideHandler:(NSNotification *)notification {
//show another viewcontroller here
NSLog(@"%@",notification.userInfo);

}

但是我没有得到任何关于谁触发了通知的信息,所以我可以针对我的特定 UITextField 而不是任何其他 UITextField 执行我的操作。

有什么想法吗?

最佳答案

您应该使用 UIGestueRecognizer。 textFieldDidBeginEditing: 方法将在您第一次按下文本字段时起作用。将点击手势识别器添加到您的文本字段:

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(testing:)];
[tapRecognizer setNumberOfTapsRequired:1];
[textField addGestureRecognizer:tapRecognizer];

现在您需要手动处理显示键盘。将此代码添加到您的测试:方法:

-(IBAction)testing:(id)sender
{
UITapGestureRecognizer *gr = (UITapGestureRecognizer*)sender;
UITextField *tf = (UITextField*)[gr view];
[tf becomeFirstResponder];
NSLog(@"testing");
}

关于ios - 按下时为 UITextfield 执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20452676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com