本文介绍了当点击UIWebView中的按钮时打开ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有UIWebView的应用程序,并且Webview包含简单的按钮
I Have an app which has UIWebView and the Webview contains simple Button
以下是我的WebViewController:
and here's my WebViewController :
import UIKit
class testViewController: UIViewController {
internal static var testID = 0
@IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let url = NSURL (string: "http://mydomainname/index.html");
let requestObj = NSURLRequest(URL: url!);
myWebView.loadRequest(requestObj);
}
}
而webView完美显示我的HTML
And the webView displaying my HTML perfectly
当我点击HTML文件中的按钮时,我只需要从我的iOS项目中提供一个newViewController?
I am just need to present a newViewController from my iOS project when i Click the button in my HTML file ?
这样:
like that :
<button onclick="myFunction()> Submit </button>
<script>
function myFunction()
{
presentViewController('myViewControllerName');
}
</script>
有没有办法在iOS中做到这一点?
is there any way to do that in iOS ?
推荐答案
是的,你可以这样做:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if (navigationType == UIWebViewNavigationType.FormSubmitted) {
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("myViewControllerName") as? myViewControllerName
let navigationController = UINavigationController(rootViewController: VC!)
self.navigationController?.presentViewController(navigationController, animated: true, completion:nil)
}
return true
}
这篇关于当点击UIWebView中的按钮时打开ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!