停止重新加载Webview

停止重新加载Webview

所以我有两个视图,一个是wkwebview,另一个是aboutus视图。在wkwebview中,我有一个按钮指向about us视图,以便用户可以阅读我们的信息。但是,当它们返回到页面时,整个页面都会刷新,我如何阻止这种情况发生?我试过检查segue.identifier,但也没有成功。

override func viewDidLoad() {
        super.viewDidLoad()

        let screenSize: CGRect = UIScreen.main.bounds
        imageViewObject = UIImageView(frame:CGRect(x:0,y: 0, width: screenSize.width, height: screenSize.height))
        imageViewObject.image = UIImage(named:"Image")
        self.view.addSubview(imageViewObject)

        webView.navigationDelegate = self
        webView.isHidden = true

        let myURL = URL(string: "https://www.google.com")
        let myRequest = URLRequest(url: myURL!)
        webView.configuration.preferences.javaScriptEnabled = true
        webView.allowsBackForwardNavigationGestures = true
        webView.load(myRequest)
    }
override func viewDidAppear(_ animated: Bool)
    {

        self.view.addSubview(webView)

    }
//this is what i tried but does not work
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "about_us_view"
        {
            webView.stopLoading();
        }
    }

关于视图控制器
import UIKit

class AboutUsViewController: UIViewController {

    var back_button: UIButton!
    var btmbar : UIImageView!
    var logo : UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        //let screenSize: CGRect = UIScreen.main.bounds

        back_button = UIButton(type: .custom)
        back_button.setImage(UIImage(named: "ic_ako_back_inactive"), for: .normal)
        back_button.frame = CGRect(x: 0, y: screenHeight - 50, width: 50, height: 50)
        back_button.addTarget(self, action: #selector(self.backButtonObj(_:)), for: .touchUpInside)
        back_button.isHidden = false

        btmbar = UIImageView(frame:CGRect(x:0 ,y: screenHeight-50, width:screenWidth, height: 80))
        btmbar.image = UIImage(named: "btm_background_bar")


        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(_ animated: Bool)
    {
        self.view.addSubview(btmbar)
        self.view.addSubview(back_button)

    }

    @objc func backButtonObj(_ sender: UIButton){ //<- needs `@objc`


        performSegue(withIdentifier: "about_us_view", sender: self)

    }
    public var screenWidth: CGFloat {
        return UIScreen.main.bounds.width
    }

    // Screen height.
    public var screenHeight: CGFloat {
        return UIScreen.main.bounds.height
    }



    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

最佳答案

你应该把这个移到viewDidLoad()

self.view.addSubview(webView)

viewWillAppear(),viewdideappear()方法在每次返回viewController时都会被调用,方法是弹出并在代码中添加新的webview实例。
编辑
在后退按钮操作时,它应该弹出viewController,而不是再次执行segue。
@objc func backButtonObj(_ sender: UIButton){
   _ = self.navigationController?.popViewController(animated: true)

}

尝试并共享结果

关于swift - 使用performSegue View 时,停止重新加载Webview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53061598/

10-09 01:27