2010-07-01 25 views
11

thể trùng lặp:
iPad: detect if external keyboard is presentLàm cách nào để phát hiện thấy bàn phím phần cứng được gắn vào iPhone?

Tôi đã đào bới xung quanh trong thư viện tham khảo và chỉ dường như không thể tìm thấy câu trả lời ở đây.

Tôi giả sử có một số API ở đâu đó mà tôi có thể truy vấn để tìm hiểu xem bàn phím phần cứng bên ngoài có đang được sử dụng hay không.

Cập nhật Tôi vừa thử các EAAccessoryManager.connectedAccessories từ ExternalAccessory.framework. Đó là một không-đi, nó trả về một mảng trống khi bàn phím phần cứng được kích hoạt.

+0

Chỉ tò mò: Tại sao bạn cần thông tin này? – Eiko

+0

Khi bàn phím phần cứng được bật, bàn phím ảo sẽ không hiển thị nữa. Chúng tôi đang thêm một datepicker như là một subview trên bàn phím cho một textfield, chúng tôi có sử dụng một ngày. Đây là loại hack do sử dụng cơ sở hạ tầng TTMessageController Ba20 chỉ cho phép UITextFields làm trường trong thư. Khi người dùng chạm vào trường văn bản "ngày" của chúng tôi, chúng tôi sẽ tìm thấy bàn phím và phủ lên UIDatePicker. – user174448

+0

Xác định xem anh ấy có cần tính đến kích thước bàn phím ảo khi cuộn đầu vào cho một hay không. – jamone

Trả lời

1

Tôi nghĩ rằng bạn phải sử dụng đoạn mã sau -

- (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]; 
} 

Hope làm việc này cho bạn và nhớ bạn phải sử dụng ExternalAccessory Khung mã này.

+0

Xcode trở nên điên rồ sau khi thêm ExternalAccessory.framework. – Dmitry

Các vấn đề liên quan