2

UIWebView如何在 iOS 6 的键盘下替换工具栏上的按钮?

以下代码在 iOS 5.1 上运行良好,但在 iOS 6 上无法运行:

UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
    if (![[testWindow class] isEqual:[UIWindow class]]) {
        keyboardWindow = testWindow;
        break;
    }
}
for (UIView *possibleFormView in [keyboardWindow subviews]) {
    // iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
    if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) {
        for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
            if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) {
                UIBarButtonItem *buttonDone =[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(pressDone)];
                NSArray *itemsArray;
                itemsArray = [NSArray arrayWithObjects:buttonDone, nil];
                [(UIToolbar*)subviewWhichIsPossibleFormView setItems:itemsArray];
            }
        }
    }
}

iOS 6 上的错误:

2012-09-27 16:31:13.537 Linux[2633:907] -[UIWebFormAccessory setItems:]: unrecognized selector sent to instance 0x1d886ad0
2012-09-27 16:31:13.540 Linux[2633:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIWebFormAccessory setItems:]: unrecognized selector sent to instance 0x1d886ad0'
*** First throw call stack:
(0x361032a3 0x3441397f 0x36106e07 0x36105531 0x3605cf68 0x775c5 0x33bbda6f 0x360d85df 0x360d8291 0x360d6f01 0x36049ebd 0x36049d49 0x365862eb 0x37428301 0x7538d 0x75328)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

非常感谢您的帮助!

4

2 回答 2

1

UIWebFormAccessory 不再是 iOS6 上的 UIToolbar。不过,它包含您正在寻找的 UIToolbar。使用类似以下的内容在任一操作系统版本上找到它...

+ (UIToolbar *)findVirginWebKeyboardToolbar:(UIView *)parent
{
    if ([parent isKindOfClass:[UIToolbar class]]) {
        // the stock toolbar contains a single item with a UISegmentedControl customView.
        UIToolbar *tb = (UIToolbar *)parent;
        if ([tb.items count] == 1 && [((UIBarButtonItem *)[tb.items objectAtIndex:0]).customView isKindOfClass:[UISegmentedControl class]]) {
            return tb;
        }
    }

    for (UIView *view in parent.subviews) {
        UIToolbar *tb = [self findVirginWebKeyboardToolbar:view];
        if (tb) return tb;
    }

    return nil;
}
于 2012-10-12T18:53:31.250 回答
0

使用以下代码删除未使用的工具栏:

 [subviewWhichIsPossibleFormView removeFromSuperview];

并添加一个新的。抱歉,没有可用的代码。

于 2012-09-27T18:24:38.470 回答