0

私は iOS が初めてで、テンキー キーボードにカスタム ボタンを追加したいので、iOS6 でキーボード ビューを見つける方法を知りたいです。次のコードを使用します。

UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
    if (![[testWindow class] isEqual:[UIWindow class]]) {
        keyboardWindow = testWindow;
        break;
    }
}

//locate the keyboard
UIView *foundKeyboard = nil;
for (UIView  __strong *possibleKeyboard in [keyboardWindow subviews]) {

    // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
    if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
        possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
    }

    if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
        foundKeyboard = possibleKeyboard;
        break;
    }
}

if (foundKeyboard) {
    // Add the button to foundKeyboard.
    [foundKeyboard addSubview:doneButton];
}

iOS 5 では正常に動作しますが、iOS 6 では動作しません。

4

2 に答える 2

2

おそらく、 (または同様のコントロール)のinputAccessoryViewプロパティを使用する必要があります。UITextFieldドキュメントから:

このプロパティのデフォルト値は nil です。システム提供の入力ビュー (キーボードなど) またはカスタム入力ビュー (inputView プロパティで提供するもの) にカスタム コントロールをアタッチするサブクラスは、このプロパティを readwrite として再宣言し、それを使用してカスタム アクセサリ ビューを管理する必要があります。 . その後、レシーバーがファーストレスポンダーになると、レスポンダー インフラストラクチャはビューを表示する前に適切な入力ビューにアタッチします。

これが私のアプリの1つからのサンプルコードです:

// Adding a UIToolbar on top of the keyboard
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.tintColor = nil;
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
[keyboardDoneButtonView sizeToFit];

// Done button on the left
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(editingDone:)];
doneButton.tintColor = TINT_COLOR;
// Creating a flexible space so the button is on the right side
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// Creating the Next button on top of the keyboard
UISegmentedControl *saveButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]];
saveButton.momentary = YES; 
saveButton.frame = CGRectMake(260, 7.0f, 120.0f, 30.0f);
saveButton.segmentedControlStyle = UISegmentedControlStyleBar;
saveButton.tintColor = [UIColor blackColor];
[saveButton addTarget:self action:@selector(moveToNextOrPrevious:) forControlEvents:UIControlEventValueChanged];
saveButton.tag = indexPath.row;
// You have to encapsulate the SegCtrl in a bar button item for it to work
UIBarButtonItem *segCtrlEnc = [[UIBarButtonItem alloc] initWithCustomView: saveButton];

// Adding the toolbar with elements
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: segCtrlEnc, flexibleSpace, doneButton, nil]];

// Plug the keyboardDoneButtonView into the text field...
cell0tf.textField.inputAccessoryView = keyboardDoneButtonView;

編集: コードは説明を目的としたものです。アプリにコピー アンド ペーストするだけでは、すぐには機能しない可能性があります。

于 2013-05-29T15:09:17.403 に答える
0

iOS 6 では、サブビューの先頭にキーボードを配置する必要はありません。次の行を置き換えてみてください。

if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
    possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
}

if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
    foundKeyboard = possibleKeyboard;
    break;
}

これ等と一緒に:

if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
    for (__strong UIView *anotherPossibleKeyboard in [possibleKeyboard subviews]) {
        if ([[anotherPossibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
            foundKeyboard = possibleKeyboard;
            break;
        }   
    }
}
于 2013-05-30T06:15:53.433 に答える