本文介绍了带有contentEditable(html编辑)的UIWebView,第一响应者处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为一个应用程序编写一个html编辑器组件(在iOS 5.0中使用带有contentEditable的UIWebView),并在如何处理UIWebView第一响应者状态方面陷入困境。
[webView isFirstResponder],[webView becomeFirstResponder]和[webView resignFirstResponder]看起来不起作用,我也不知道如何让webView成为代码或者通过代码来退出它。
如果有人知道如何解决这个问题,我会非常感激,提前致谢!
在
UIWebView
子类中覆盖这些方法(
content
是可编辑元素的id):
- (BOOL)resignFirstResponder {
[self setUserInteractionEnabled:NO]; [self setUserInteractionEnabled:YES];
return [super resignFirstResponder];
}
//仅适用于iOS 6+
- (void)becomeFirstResponder {
self.keyboardDisplayRequiresUserAction = NO; //在此设置或在初始化期间设置
[self stringByEvaluatingJavaScriptFromString:@document.getElementById('content')。focus()];
$ b $(BOOL)isFirstResponder {
if([[self stringByEvaluatingJavaScriptFromString:@document.activeElement.id =='content'] isEqualToString:@true ]){
return YES;
}
else {
return NO;
isFirstResponder
只会在显示键盘后返回true(例如,它会在UIKeyboardWillShowNotification返回false)
如果这是个问题,另一种方法是检查 UIWebView
是第一个响应者,如下所示:
+(BOOL) isFirstResponder:(UIView *)v {
for(UIView * vs in v.subviews){
if([vs isFirstResponder] || [self isFirstResponder:vs]){
return YES;
}
}
return NO;
}
- (BOOL)isFirstResponder {
return [[self class] isFirstResponder:self];
}
这样,返回的值就是 YES
甚至在键盘动画完成(显示或隐藏)之前/之后。
I'm making an html editor component for an app (using UIWebView with contentEditable in iOS 5.0), and got stuck at how to handle UIWebView first responder status
[webView isFirstResponder], [webView becomeFirstResponder] and [webView resignFirstResponder] don't seem to work, and i've no idea how to make the webView become or resign it by code
If anyone knows how to work this out i would be very grateful, thanks in advance!
解决方案
Here is how I overwrite these methods in a UIWebView
subclass (content
is the id of the editable element):
-(BOOL)resignFirstResponder {
[self setUserInteractionEnabled:NO];[self setUserInteractionEnabled:YES];
return [super resignFirstResponder];
}
// only works on iOS 6+
-(void)becomeFirstResponder {
self.keyboardDisplayRequiresUserAction = NO; // set here or during initialization
[self stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').focus()"];
}
-(BOOL)isFirstResponder{
if ([[self stringByEvaluatingJavaScriptFromString:@"document.activeElement.id=='content'"] isEqualToString:@"true"]) {
return YES;
}
else {
return NO;
}
}
isFirstResponder
will only return true after the keyboard is shown (e.g, it will return false at UIKeyboardWillShowNotification)
In case this is an issue, another way to check if the UIWebView
is the first responder is as follows:
+(BOOL)isFirstResponder:(UIView *)v{
for (UIView *vs in v.subviews) {
if ([vs isFirstResponder] || [self isFirstResponder:vs]) {
return YES;
}
}
return NO;
}
-(BOOL)isFirstResponder{
return [[self class] isFirstResponder:self];
}
This way, the returned value will be YES
even before/after the keyboard animation finishes (showing or hiding).
这篇关于带有contentEditable(html编辑)的UIWebView,第一响应者处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!