简体   繁体   English

没有textview的iphone键盘

[英]iphone keyboard without textview

is it possible to bring up the keyboard in an iphone app without a textview? 是否可以在没有textview的iPhone应用程序中调出键盘? or will i have to have an invisible textview? 或者我必须有一个看不见的textview?

if so, how do you programatically create a textview and then bring up the keyboard (without the user having to tap the textview)? 如果是这样,你如何以编程方式创建文本视图,然后调出键盘(用户不必点击文本视图)? the only examples i can find use interface builder.. 我能找到的唯一例子是使用界面构建器..

The only (valid) way to show the keyboard is to have a textfield that is first responder. 显示键盘的唯一(有效)方法是使文本字段成为第一响应者。 You can hide it and make it first responder programmatically by calling becomeFirstResponder on the hidden textfield. 您可以通过在隐藏文本becomeFirstResponder上调用becomeFirstResponder来隐藏它并以编程方式使其成为第一响应者。

You can create a UITextView programmatically by doing something like this (assume aRect and view exist) 您可以通过执行类似的操作以编程方式创建UITextView(假设存在aRect和视图)

var textView = [[[UITextView alloc] initWithFrame:aRect] autorelease];
[view addSubview:textView];

[textView becomeFirstResponder];

After some more digging, I found this . 经过一番挖掘,我发现了这一点 It's unofficial, but I bet it works. 这是非官方的,但我敢打赌它有效。

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];

The way this stuff works is via the NSNotificationCenter publish/subscribe model. 这些东西的工作方式是通过NSNotificationCenter发布/订阅模型。 First you need to use addObserver:selector:name:object: , then you can try doing this : 首先,你需要使用addObserver:selector:name:object: ,那么你可以尝试做这个

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:NSTextDidBeginEditingNotification object:self]];

But I'm not sure what notifications you would get, or would need to register for, to get the keyboard typing character values. 但我不确定您将获得或需要注册的通知,以获取键盘输入字符值。 Good luck and happy hacking :) 祝你好运和快乐的黑客:)

UIKeyInput is your friend: 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
        }
    }
}

Example usage. 用法示例。 Tap the red view and see the Xcode console output: 点击红色视图,查看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)")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM