gpt4 book ai didi

swift - 使用 performSegue View 时停止重新加载 webview

转载 作者:行者123 更新时间:2023-11-28 07:37:15 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com