iphone keyboard without textview
Asked Answered
P

4

6

is it possible to bring up the keyboard in an iphone app without a textview? or will i have to have an invisible 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..

Pineapple answered 24/9, 2009 at 14:56 Comment(2)
why would you want to 'bring up' the keyboard if there's nowhere to enter text?Rosebay
possible duplicate of How to pull up a UIKeyboard without a UITextField or UITextView?Hackery
C
10

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.

You can create a UITextView programmatically by doing something like this (assume aRect and view exist)

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

[textView becomeFirstResponder];
Calling answered 24/9, 2009 at 15:1 Comment(0)
O
5

UIKeyInput is your friend:

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:

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)")
    }
}
Orfurd answered 16/9, 2015 at 12:0 Comment(0)
O
2

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];
Olsen answered 24/9, 2009 at 16:30 Comment(0)
O
1

The way this stuff works is via the NSNotificationCenter publish/subscribe model. First you need to use addObserver:selector:name:object:, then you can try doing this:

[[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 :)

Olsen answered 24/9, 2009 at 15:11 Comment(3)
How will this bring up the keyboard?Calling
I'm not sure if it will, the trick is to find the right notification to postOlsen
more keyboard notification stuff iphonedevsdk.com/forum/iphone-sdk-development/…Olsen

© 2022 - 2024 — McMap. All rights reserved.