UIWebview的键盘返回键按下ios 7

我试图找出一个解决scheme,当UIWebview的返回键被点击! 我尝试了以下链接,但它不工作!

UIWebView,自定义“返回”键

如何检测键盘input密钥?

我想做一些偏移工作,当用户在UIWebview上键入并按下返回键!

这是我使用的代码!

- (void)viewDidLoad { [super viewDidLoad]; NSString* plainContent = @"Edit here....."; NSString* htmlContentString = [NSString stringWithFormat: @"<html>" "<body>" "<div id=\"content\" contenteditable=\"true\" style=\"font-family: Arial\">" "%@" "</div>" "</body></html>", plainContent]; [_webview loadHTMLString:htmlContentString baseURL:nil]; } - (BOOL)webView:(UIWebView*)aWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{ NSString *requestString = [[request URL] absoluteString]; NSArray *components = [requestString componentsSeparatedByString:@":"]; NSString *theTask = (NSString *)[components objectAtIndex:0]; if([[[request URL] absoluteString] rangeOfString:@"enterClicked"].location!=NSNotFound) // When "enterClicked" found { NSLog(@"enterClicked !"); //Do your desired work return NO; } if([theTask isEqualToString:@"returnkeypressed"]){ NSLog(@"theTask"); [aWebView endEditing:YES]; return NO; } return YES; } 

为什么UIWebView的回答,自定义“返回”键不起作用?

我尝试和为我工作,我只改变方法shouldStartLoadWithRequest:到:

 - (BOOL)webView:(UIWebView*)aWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{ NSString *requestString = [[request URL] absoluteString]; NSArray *components = [requestString componentsSeparatedByString:@":"]; NSString *theTask = (NSString *)[components objectAtIndex:0]; if([theTask isEqualToString:@"returnkeypressed"]){ [aWebView endEditing:YES]; return NO; } return YES; } 

另一种方法是使用缓冲区文本视图,就像我的答案改变键盘。

编辑:你需要添加脚本来检测返回键:

 NSString* plainContent = @"Edit here....."; NSString* htmlContentString = [NSString stringWithFormat: @"<html>" "<head>" "<script>" "function returnKeyPressed(event){" "if(window.event.keyCode == 13)" "document.location = \"returnkeypressed:\";" "return true;" "}" "</script>" "</head>" "<body onKeyPress=\"return returnKeyPressed(event)\">" "<div id=\"content\" contenteditable=\"true\" style=\"font-family: Arial\">%@" "</div>" "</body>" "</html>", plainContent]; 

textView: shouldChangeTextInRange:插入这段代码textView: shouldChangeTextInRange:

 if ([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; }