使用 performSegue 视图时停止重新加载 webview

Stop webview from reloading when performSegue view is used

所以我有 2 个视图,一个是 wkwebview,另一个是 aboutus 视图。在 wkwebview 中,我有一个按钮可以转到关于我们的视图,以便用户可以阅读有关我们的信息。但是,当他们 return 返回页面时,整个页面都会刷新,我该如何阻止这种情况发生?我曾尝试检查 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();
        }
    }

关于我们viewcontroller

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()、viewDidAppear() 方法在每次您 return 到 viewController 时通过弹出并因此在您的代码中添加新的 webview 实例时被调用。

编辑

在后退按钮操作上,它应该弹出 viewController,而不是再次执行 segue。

@objc func backButtonObj(_ sender: UIButton){ 
   _ = self.navigationController?.popViewController(animated: true)

}

尝试并分享结果