11

我有一个蓝牙条码设备。如果将蓝牙设备连接到 iPhone,我无法使用 iPhone 键盘写任何东西。您已经知道 iPhone 键盘不显示,因为蓝牙设备是识别键盘。

但!!!当 iphone 与蓝牙设备连接时,我必须通过键盘在文本框中写一些东西。

请让我知道该怎么做!:) 谢谢~

4

2 回答 2

13

即使连接了蓝牙键盘,我们也可以显示设备虚拟键盘。我们需要使用inputAccessoryView它。

我们需要在 app delegate.h 中添加以下代码

@property (strong, nonatomic) UIView *inputAccessoryView;

在delegate.m(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中的方法中添加以下通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBegan:) name:UITextFieldTextDidBeginEditingNotification object:nil];

当我们关注 a 时,这将调用下面的方法textField

//This function responds to all `textFieldBegan` editing
// we need to add an accessory view and use that to force the keyboards frame
// this way the keyboard appears when the bluetooth keyboard is attached.
-(void) textFieldBegan: (NSNotification *) theNotification
{

        UITextField *theTextField = [theNotification object];

        if (!inputAccessoryView) {
            inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
            [inputAccessoryView setBackgroundColor:[UIColor lightGrayColor]];
        }

        theTextField.inputAccessoryView = inputAccessoryView;

        [self performSelector:@selector(forceKeyboard) withObject:nil afterDelay:0];
}

“forceKeyboard”的代码是,

-(void) forceKeyboard
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    inputAccessoryView.superview.frame = CGRectMake(0, 420, screenHeight, 352);

}

这对我们来说很好。我们使用隐藏的文本字段从蓝牙键盘获取输入,对于所有其他文本字段,我们使用设备虚拟键盘,使用inputAccessoryView.

请让我知道这是否有帮助,如果您需要更多详细信息。

于 2013-12-17T18:50:37.020 回答
0

创建一个 UIView 子类遵循 UIKeyInput 协议。

@interface SomeInputView : UIView <UIKeyInput> {

并在实现文件(.m)中

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)insertText:(NSString *)text {
    //Some text entered by user
}

-(void)deleteBackward {
    //Delete key pressed
}

每当您想显示键盘时

[myInputView becomeFirstResponder];
于 2016-04-18T05:46:54.260 回答