How to show the keyboard for a UIView

喜夏-厌秋 提交于 2019-12-10 02:24:57

问题


I have a UIView where I want to receive keyboard input. I have tried:

[self becomeFirstResponder];

But it doesn't work.

I could implement a 'dirty' workaround by having a hidden UITextField and forwarding the keystokes to my UIView - but how does my UIView receive the keyboard input directly?


回答1:


For a view to become first responder and show the keyboard, it must adopt and implement the UIKeyInput protocol. It must also override the canBecomeFirstResponder method to return YES.

See “Simple Text Input” in the Text Programming Guide for iOS.




回答2:


Becoming a first responder will only show the keyboard if that object is meant for keyboard input like a text field. The idea of having a hidden text field is good but it also depends on what you need the keyboard input for, regarding your view. Getting the text form the hidden text field is trivial and that way the text or whatever you need there, can be used by your view.



来源:https://stackoverflow.com/questions/10819707/how-to-show-the-keyboard-for-a-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!