我有一個藍牙條形碼設備。 如果將藍牙設備連接到iPhone,我無法使用iPhone鍵盤輸入任何內容。 你已經知道iPhone鍵盤沒有顯示,因爲藍牙設備是被識別的鍵盤。我想用藍牙設備強制鍵盤
但是!!!當iPhone連接藍牙設備時,我必須通過鍵盤將文字寫入文本框。
請讓我知道該怎麼做! :) 謝謝〜
我有一個藍牙條形碼設備。 如果將藍牙設備連接到iPhone,我無法使用iPhone鍵盤輸入任何內容。 你已經知道iPhone鍵盤沒有顯示,因爲藍牙設備是被識別的鍵盤。我想用藍牙設備強制鍵盤
但是!!!當iPhone連接藍牙設備時,我必須通過鍵盤將文字寫入文本框。
請讓我知道該怎麼做! :) 謝謝〜
即使連接了藍牙鍵盤,我們也可以顯示設備虛擬鍵盤。我們需要使用inputAccessoryView
。
我們需要添加以下代碼的應用程序delegate.h
@property (strong, nonatomic) UIView *inputAccessoryView;
在下面添加通知 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法delegate.m
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBegan:) name:UITextFieldTextDidBeginEditingNotification object:nil];
這將調用下面的方法時,我們關注在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
顯示。
請讓我知道這是否有幫助,如果您需要任何更多的細節。
按照UIKeyInput協議創建一個UIView子類。
@interface SomeInputView : UIView <UIKeyInput> {
和實現文件(.M)
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)insertText:(NSString *)text {
//Some text entered by user
}
-(void)deleteBackward {
//Delete key pressed
}
每當你想顯示的鍵盤只是做
你找到任何解決方案?這裏同樣的問題! – kokemomuke