2010-07-01 65 views
11

可能重複:
iPad: detect if external keyboard is present如何檢測硬件鍵盤是否連接到iPhone?

我已經在基準庫被挖四周,似乎無法在這裏找到答案。

我假設有一些API可以查詢我是否正在使用外部硬件鍵盤。

更新 我剛剛從ExternalAccessory.framework中嘗試了EAAccessoryManager.connectedAccessories。這是一個不行,當硬件鍵盤被啓用時它返回一個空數組。

+0

只是好奇:你爲什麼需要這些信息? – Eiko 2010-07-01 15:33:24

+0

啓用硬件鍵盤後,屏幕鍵盤不再顯示。我們添加一個日期選擇器作爲我們使用日期的文本框的鍵盤上的子視圖。由於使用了Three20 TTMessageController基礎結構,只允許將UITextFields作爲消息中的字段,這就是一種破解。當用戶觸摸我們的「日期」文本字段時,我們會找到鍵盤並覆蓋UIDatePicker。 – user174448 2010-07-01 15:54:54

+0

確定滾動輸入時是否需要考慮虛擬鍵盤的大小。 – jamone 2010-07-01 15:55:17

回答

1

我認爲你必須使用下面的代碼 -

- (void)viewDidLoad { 
    UIView* _noExternalAccessoriesPosterView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    [_noExternalAccessoriesPosterView setBackgroundColor:[UIColor whiteColor]]; 
    _noExternalAccessoriesLabelView = [[UILabel alloc] initWithFrame:CGRectMake(60, 170, 240, 50)]; 
    [_noExternalAccessoriesLabelView setText:@"No Accessories Connected"]; 
    [_noExternalAccessoriesPosterView addSubview:_noExternalAccessoriesLabelView]; 
    [[self view] addSubview:_noExternalAccessoriesPosterView]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil]; 
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications]; 

    _eaSessionController = [EADSessionController sharedController]; 
    _accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]]; 

    [self setTitle:@"Accessories"]; 

    if ([_accessoryList count] == 0) { 
     [_noExternalAccessoriesPosterView setHidden:NO]; 
    } else { 
     [_noExternalAccessoriesPosterView setHidden:YES]; 
    } 
} 

- (void)_accessoryDidConnect:(NSNotification *)notification { 
    EAAccessory *connectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey]; 
    [_accessoryList addObject:connectedAccessory]; 

    if ([_accessoryList count] == 0) { 
     [_noExternalAccessoriesPosterView setHidden:NO]; 
    } else { 
     [_noExternalAccessoriesPosterView setHidden:YES]; 
    } 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([_accessoryList count] - 1) inSection:0]; 
    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
} 

希望這對你的作品,並記住你必須使用ExternalAccessory框架的代碼。

+0

添加ExternalAccessory.framework後,Xcode變得瘋狂。 – Dmitry 2015-02-17 21:17:35

相關問題