【问题标题】:Stop webview from reloading when performSegue view is used使用 performSegue 视图时停止 webview 重新加载
【发布时间】:2018-10-30 09:54:23
【问题描述】:

所以我有 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();
        }
    }

关于我们的视图控制器

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

}

【问题讨论】:

  • 我在您的代码中发现了几个错误。当您覆盖 viewDidAppear(_ animated: Bool) 函数时,您不会调用 super.viewDidAppear。如果你在viewDidAppear 中调用view.addSubview(webView),你在哪里调用webView.removeFromSuperview()
  • @AlexSmet 你是什么意思?我对此有点陌生。

标签: swift xcode segue wkwebview back


【解决方案1】:

你应该把它移到viewDidLoad()

self.view.addSubview(webView)

viewWillAppear(), viewDidAppear() 方法会在你每次返回 viewController 时被调用,并在你的代码中添加新的 webview 实例。

编辑

在返回按钮动作时,它应该弹出 viewController,而不是再次执行 segue。

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

}

尝试并分享结果

【讨论】:

  • 当我这样做时,它仍然是一样的。它仍然加载
  • 我想换个东西吗?当我用 perform segue 替换你的代码时,什么也没有发生
  • AboutUsViewController 中的 backButtonObj(_ sender: UIButton) 换了吗?
  • 嘿,这行得通,但你必须先这样做。检查它是否嵌入到导航控制器中,尝试:点击 StroryBoard 选择第一个 viewController 点击编辑器 -> 嵌入 -> 导航控制器 希望它有帮助。
  • 你必须在导航控制器中添加嵌入,然后导航控制器类检测到它并可以弹出视图控制器,谢谢!在你的帮助下不可能做到这一点
猜你喜欢
  • 2014-07-21
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
相关资源
最近更新 更多