不带文本视图的iphone键盘

不带文本视图的iphone键盘,iphone,keyboard,iphone-softkeyboard,Iphone,Keyboard,Iphone Softkeyboard,有没有可能在没有文本视图的情况下打开iphone应用程序中的键盘?或者我必须有一个不可见的文本视图 如果是这样,您如何通过编程创建文本视图,然后打开键盘(用户无需点击文本视图)?我能找到的唯一示例是使用界面生成器。显示键盘的唯一(有效)方法是使用一个文本字段作为第一响应者。 您可以通过调用隐藏文本字段上的becomeFirstResponder以编程方式隐藏它并使其成为第一响应程序 您可以通过如下操作以编程方式创建UITextView(假设存在aRect和视图) 这些东西的工作方式是通过发布/订

有没有可能在没有文本视图的情况下打开iphone应用程序中的键盘?或者我必须有一个不可见的文本视图

如果是这样,您如何通过编程创建文本视图,然后打开键盘(用户无需点击文本视图)?我能找到的唯一示例是使用界面生成器。

显示键盘的唯一(有效)方法是使用一个文本字段作为第一响应者。 您可以通过调用隐藏文本字段上的
becomeFirstResponder
以编程方式隐藏它并使其成为第一响应程序

您可以通过如下操作以编程方式创建UITextView(假设存在aRect和视图)


这些东西的工作方式是通过发布/订阅模型。首先需要使用
addObserver:selector:name:object:
,然后可以尝试执行以下操作:


但我不确定你会收到什么通知,或者需要注册什么通知才能获得键盘输入字符值。祝你好运,黑客快乐:)

经过进一步挖掘,我发现了。这是非官方的,但我敢打赌它是有效的

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
        }
    }
}

如果没有地方输入文本,你为什么要“打开”键盘?这可能会重复如何打开键盘?我不确定是否会,诀窍是找到正确的通知来发送后期键盘通知
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];
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
        }
    }
}
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)")
    }
}