UIInputViewController can't response action that come from UIButton before iOS10

114 Views Asked by At

I build a custom keyboard by codes. On the self.inputView, add some buttons to input custom content. the core codes as follows:

[self.wordsSenders enumerateObjectsUsingBlock:^(UIButton * _Nonnull cell, NSUInteger idx, BOOL * _Nonnull stop) {
    [self.inputView addSubview:cell];
}];

all buttons can display exactly. In iOS 10, inputViewController can response button's action to input word. but in iOS 8.2 or iOS 9.3, press button will lead to crash because unrecognized selector send to 0xXXXX. MeanWhile, the object, receive button's action, is different. the inputViewController's inputWord: isn't trigger!

- (void)inputWord:(UIButton *)sender {
if ([sender.titleLabel.text isEqualToString:@"字"]) {
    _showSpecialWords = !_showSpecialWords;

    for (int i = 20; i < 28; i ++) {
        if (_showSpecialWords) {
            [self.wordsSenders[i] setTitle:self.specialWords[i - 20] forState:UIControlStateNormal];
        } else {
            [self.wordsSenders[i] setTitle:self.normalWords[i] forState:UIControlStateNormal];
        }
    }
} else {
    [self.textDocumentProxy deleteBackward];
    [self.textDocumentProxy insertText:sender.titleLabel.text];
}
}

for button binding action:

[cell addTarget:self action:@selector(inputWord:) forControlEvents:UIControlEventTouchUpInside];

Any help? Thanks!

1

There are 1 best solutions below

0
On

Oh, sorry, It's my mistake! I should maintain a instance variable about UIInputViewController rather than build a local variable. In iOS 10, it can work exactly It may be because system automaticly maintain UIInputViewController instance and iOS 8 or iOS 9 doesn't. just like this:

CGRect viewFrame = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - CGRectGetMaxY(_finishButton.frame) - minusHeight);

_carPlateViewController = [[EPCarPlateViewController alloc] initWithKeyboardType:type hiddenType:hiddenType];
_carPlateViewController.delegate = self;

UIView *inputView = _carPlateViewController.inputView;
inputView.frame = viewFrame;

textField.inputView = inputView;