iphone keyboard without textview

后端 未结 4 1002
难免孤独
难免孤独 2020-12-15 01:22

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

相关标签:
4条回答
  • 2020-12-15 01:49

    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)")
        }
    }
    
    0 讨论(0)
  • 2020-12-15 02:02

    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];
    
    0 讨论(0)
  • 2020-12-15 02:03

    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];
    
    0 讨论(0)
  • 2020-12-15 02:08

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

    0 讨论(0)
提交回复
热议问题