【问题标题】:iphone keyboard without textview没有textview的iphone键盘
【发布时间】:2010-12-01 03:08:44
【问题描述】:

是否可以在没有文本视图的情况下在 iphone 应用程序中调出键盘?还是我必须有一个不可见的文本视图?

如果是这样,您如何以编程方式创建一个文本视图,然后调出键盘(无需用户点击文本视图)?我能找到的唯一示例使用界面生成器..

【问题讨论】:

标签: iphone keyboard iphone-softkeyboard


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    经过一番挖掘,我找到了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];
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

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

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

        但我不确定您会收到或需要注册哪些通知才能获取键盘输入字符值。祝你好运,黑客愉快:)

        【讨论】:

        猜你喜欢
        • 2011-03-25
        • 2011-11-01
        • 1970-01-01
        • 2012-02-10
        • 2011-10-31
        • 2018-08-08
        • 1970-01-01
        • 2015-10-25
        • 1970-01-01
        相关资源
        最近更新 更多