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

时间:2018-10-30 09:54:23

标签: swift xcode segue wkwebview back

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

aboutusviewcontroller

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.
    }
    */

}

1 个答案:

答案 0 :(得分:0)

您应将此移动到viewDidLoad()

self.view.addSubview(webView)
每次返回viewController时,都会弹出

viewWillAppear(),viewDidAppear()方法,方法是弹出并在代码中添加新的Webview实例。

编辑

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

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

}

尝试并分享结果