是否可以在沒有textview的iPhone應用程序中調出鍵盤?或者我將不得不有一個隱形的TextView?沒有textview的iphone鍵盤
如果是這樣,你如何編程創建一個文本視圖,然後調出鍵盤(用戶不必點擊文本視圖)?唯一的例子,我可以找到使用接口生成器..
是否可以在沒有textview的iPhone應用程序中調出鍵盤?或者我將不得不有一個隱形的TextView?沒有textview的iphone鍵盤
如果是這樣,你如何編程創建一個文本視圖,然後調出鍵盤(用戶不必點擊文本視圖)?唯一的例子,我可以找到使用接口生成器..
唯一(有效)的方式來顯示鍵盤是有一個文本字段是第一響應者。 您可以通過在隱藏文本框中調用becomeFirstResponder
來隱藏它,並以編程方式使其成爲第一響應者。
你可以做這樣的事情(假設aRect和觀點存在)
var textView = [[[UITextView alloc] initWithFrame:aRect] autorelease];
[view addSubview:textView];
[textView becomeFirstResponder];
這個東西的工作方式編程方式創建一個UITextView是通過NSNotificationCenter發佈/訂閱模式。首先,你需要使用addObserver:selector:name:object:
,那麼你可以嘗試做this:
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:NSTextDidBeginEditingNotification object:self]];
但我不知道哪些通知會得到,或需要進行註冊,以獲取鍵盤輸入字符值。祝你好運,快樂的黑客:)
這將如何顯示鍵盤? – klaaspieter 2009-09-24 15:14:11
我不確定它是否會,訣竅是找到正確的通知發佈 – slf 2009-09-24 16:11:15
更多的鍵盤通知的東西http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top -keyboard.html – slf 2009-09-24 16:12:01
經過一些挖掘,我發現this。這是非官方的,但我敢打賭它是有效的。
UIKeyboard *keyboard = [[[UIKeyboard alloc] initWithFrame: CGRectMake(0.0f, contentRect.size.height - 216.0f, contentRect.size.width, 216.0f)] autorelease];
[keyboard setReturnKeyEnabled:NO];
[keyboard setTapDelegate:editingTextView];
[inputView addSubview:keyboard];
UIKeyInput
是你的朋友:
protocol KeyboardInputControlDelegate: class {
func keyboardInputControl(keyboardInputControl:KeyboardInputControl, didPressKey key:Character)
}
class KeyboardInputControl: UIControl, UIKeyInput {
// MARK: - properties
weak var delegate: KeyboardInputControlDelegate?
// MARK: - init
override init(frame: CGRect) {
super.init(frame: frame)
addTarget(self, action: Selector("onTouchUpInside:"), forControlEvents: .TouchUpInside)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - UIView
override func canBecomeFirstResponder() -> Bool {
return true
}
// MARK: - methods
dynamic private func onTouchUpInside(sender: KeyboardInputControl) {
becomeFirstResponder()
}
// MARK: - UIKeyInput
var text:String = ""
func hasText() -> Bool {
return text.isEmpty
}
func insertText(text: String) {
self.text = text
for ch in text {
delegate?.keyboardInputControl(self, didPressKey: ch)
}
}
func deleteBackward() {
if !text.isEmpty {
let newText = text[text.startIndex..<text.endIndex.predecessor()]
text = newText
}
}
}
用法示例。點擊紅色視圖並查看Xcode控制檯輸出:
class ViewController: UIViewController, KeyboardInputControlDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let kic = KeyboardInputControl(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
kic.delegate = self
kic.backgroundColor = UIColor.redColor()
view.addSubview(kic)
}
func keyboardInputControl(keyboardInputControl: KeyboardInputControl, didPressKey key: Character) {
println("Did press: \(key)")
}
}
UIKeyInput
是解決方案的關鍵。
protocol KeyboardInputControlDelegate: class {
func keyboardInputControl(keyboardInputControl:KeyboardInputControl, didPressKey key:Character)
}
class KeyboardInputControl: UIControl, UIKeyInput {
// MARK: - properties
weak var delegate: KeyboardInputControlDelegate?
// MARK: - init
override init(frame: CGRect) {
super.init(frame: frame)
addTarget(self, action: Selector("onTouchUpInside:"), forControlEvents: .TouchUpInside)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - UIView
override func canBecomeFirstResponder() -> Bool {
return true
}
// MARK: - methods
dynamic private func onTouchUpInside(sender: KeyboardInputControl) {
becomeFirstResponder()
}
// MARK: - UIKeyInput
var text:String = ""
func hasText() -> Bool {
return text.isEmpty
}
func insertText(text: String) {
self.text = text
for ch in text {
delegate?.keyboardInputControl(self, didPressKey: ch)
}
}
func deleteBackward() {
if !text.isEmpty {
let newText = text[text.startIndex..<text.endIndex.predecessor()]
text = newText
}
}
}
示例用法。點擊紅色的視圖,看到Xcode控制檯輸出:
class ViewController: UIViewController, KeyboardInputControlDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let kic = KeyboardInputControl(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
kic.delegate = self
kic.backgroundColor = UIColor.redColor()
view.addSubview(kic)
}
func keyboardInputControl(keyboardInputControl: KeyboardInputControl, didPressKey key: Character) {
println("Did press: \(key)")
}
}
爲什麼你要'調出'鍵盤如果沒有地方輸入文本? – 2009-09-24 14:57:06
[如何拉起沒有UITextField或UITextView的UIKeyboard?](http://stackoverflow.com/questions/1376120/how-to-pull-up-a-uikeyboard-without-a-uitextfield-or -uitextview) – JosephH 2014-04-02 10:58:27