xcode 检测用户是否按下了 Go 或隐藏键盘按钮 UITextField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10151426/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:58:22  来源:igfitidea点击:

detecting if User pressed Go or hide keyboard button UITextField

iphoneobjective-ciosxcode

提问by Shubhank

I am making a browser type app..

我正在制作一个浏览器类型的应用程序..

Now i have made the Url entering textfield...which shows up the keyboard..

现在我已经让 Url 输入文本字段......它显示了键盘......

Now i have made it be Url type keyboard..it has Go button and for iPad a button to hide the keyboard..

现在我已经把它变成了 Url 类型的键盘..它有 Go 按钮,而 iPad 有一个隐藏键盘的按钮..

-(BOOL)textFieldShouldReturn:(UITextField *)textField

i know above is the method that is called when user clicks return on Keyboard.. but for iPad ..there are two different things..i want to go the new URL if user presses Go..and leave the window as it is if user presses hide keyboard..but the problem is both the events report

我知道上面是用户在键盘上单击返回时调用的方法.. 但对于 iPad.. 有两种不同的事情..如果用户按 Go.. 我想转到新 URL 并保持窗口原样用户按下隐藏键盘..但问题是两个事件报告

-(BOOL)textFieldShouldReturn:(UITextField *)textField

so how do i differentiate user pressed Go ..or just hide keyboard..

那么我如何区分用户按下的 Go .. 或者只是隐藏键盘..

回答by Nikita Pestrov

When the user taps Hide button, the UIKeyboardWillHideNotificationgets called,so you should register for observing it

当用户点击隐藏按钮时,UIKeyboardWillHideNotification被调用,所以你应该注册以观察它

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


- (void) keyboardWillHideHandler:(NSNotification *)notification {
    //show another viewcontroller here
}

回答by Shubhank

Apparently now it seems that the Go button only reports

显然现在似乎 Go 按钮只报告

-(BOOL)textFieldShouldReturn:(UITextField *)textField

and hide keyboard button doesn't..

并隐藏键盘按钮不会..

I am pretty sure yesterday this was not the case..but anyways..i believe i am only to blame for this mess up..i will leave the question open if anyone needs clearing.

我很确定昨天情况并非如此..但无论如何..我相信我只能为这个混乱负责..如果有人需要清理,我会留下这个问题。

回答by jyoti

You try to use touch function with key board I think it will work.

您尝试在键盘上使用触摸功能,我认为它会起作用。

回答by meaning-matters

At least on iOS 12 textFieldDidEndEditing(_:)is called when tapping the hide button (on iPad).

至少在 iOS 12 textFieldDidEndEditing(_:)上点击隐藏按钮(在 iPad 上)会被调用。

Hope this helps.

希望这可以帮助。