我的swift应用程序中有一个webview,它连接的网站上有很多链接。按下链接时,如何进行此操作而不是在同一视图中打开链接,使用后退按钮(导航控制器)在新视图中打开链接。我的viewcontroller.swift代码如下。有什么帮助吗?
import UIKit
import Parse
import PKHUD
class Home: UIViewController {
@IBOutlet var homeweb: UIWebView!
@IBOutlet var HomeActivity: UIActivityIndicatorView!
var request: NSURLRequest? = NSURLRequest(URL: NSURL(string: "https://google.com")!)
override func viewDidLoad() {
super.viewDidLoad()
if navigationController?.viewControllers.count > 1 { navigationItem.leftBarButtonItem = nil }
PKHUD.sharedHUD.contentView = PKHUDSuccessView()
PKHUD.sharedHUD.show()
PKHUD.sharedHUD.dimsBackground.boolValue
PKHUD.sharedHUD.userInteractionOnUnderlyingViewsEnabled.boolValue
PKHUD.sharedHUD.hide(afterDelay: 3.0);
if let request = request {
homeweb.loadRequest(request)
}
}
}
extension Home: UIWebViewDelegate {
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request != self.request && navigationType != .Other {
if let Home = storyboard?.instantiateViewControllerWithIdentifier("homeview") as? Home {
Home.request = request
navigationController?.pushViewController(Home, animated: true)
return false
}
}
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(homeweb: UIWebView)
{
HomeActivity.startAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func webViewDidFinishLoad(homeweb: UIWebView)
{
HomeActivity.stopAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
title = homeweb.stringByEvaluatingJavaScriptFromString("document.title")
}
@IBAction func about(sender: UIBarButtonItem) {
let alertController = UIAlertController(title: "About Ball00n", message: "Copyright ©2014-2015 GreenBalloon, LLC James Cozzi (jc0z) jamescozzi00@gmail.com balloonhack.webs.com", preferredStyle: .Alert)
let Action1 = UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil)
alertController.addAction(Action1)
self.presentViewController(alertController, animated: true, completion: nil)
}
}
答案 0 :(得分:2)
将viewcontroller设置为webview的委托,并实现以下方法:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
// check for your specific condition
if request.URL?.absoluteString == "some link" {
// push viewcontroller here or perform a segue
return false // to not follow the link in the webview
}
return true
}
现在 - 在了解了你真正想要实现的目标之后:) - 我做了以下事情:
在故事板中设置一个viewcontroller,只需在整个屏幕上显示webview。 ctrl从webview拖动到webviewcontroller并将其设置为其委托。将viewcontroller设置为自定义类“WebViewController”。还将故事板ID设置为“WebViewController”。从webview拖到WebViewController类并连接一个名为“webView”的IBOutlet。最后,您的WebViewController类应如下所示:
import UIKit
class WebViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
var request: NSURLRequest? = NSURLRequest(URL: NSURL(string: "http://YOUR_INITIAL_WEBSITE")!)
override func viewDidLoad() {
super.viewDidLoad()
if navigationController?.viewControllers.count > 1 {
navigationItem.leftBarButtonItem = nil
}
if let request = request {
webView.loadRequest(request)
}
}
}
extension WebViewController: UIWebViewDelegate {
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request != self.request && navigationType != .Other {
if let webViewController = storyboard?.instantiateViewControllerWithIdentifier("WebViewController") as? WebViewController {
webViewController.request = request
navigationController?.pushViewController(webViewController, animated: true)
return false
}
}
return true
}
func webViewDidFinishLoad(webView: UIWebView) {
title = webView.stringByEvaluatingJavaScriptFromString("document.title")
}
}
至少这是一个开始:)