gpt4 book ai didi

swift - 当按下事件标签栏时如何加载 WKWebview 转到根页面?

转载 作者:行者123 更新时间:2023-11-30 10:47:33 24 4
gpt4 key购买 nike

我是使用 Xcode 10.2 和 Swift 5(最新更新)进行 iOS 开发的新手。我正在使用 TabBar 和 WKWebview 开发 iOS 应用程序。该应用程序有 5 个选项卡,每个选项卡显示不同的网页。

我想让用户体验到,当用户访问第一个选项卡并浏览那里时,然后用户按另一个选项卡栏,之后用户返回第一个选项卡并在转到另一个选项卡之前仍然显示相同的页面。如果用户想返回选项卡的根页面,只需再次按事件选项卡栏即可。

我怎样才能实现它?

我正在使用 RootViewController,然后扩展到 5 个另一个 ViewController:
- HomeViewController
- SignalViewController
-ContactViewController
- NewsViewController
- ProfileViewController

我尝试了许多网站的一些建议但失败了,例如使用:
- UITabBarControllerDelegate tabBarController(:didSelect:)
- UIViewController viewDidAppear(
:)
- UIViewController viewWillAppear(:)
- super.viewDidLoad() 或 self.viewDidLoad()
- UIView setNeedsDisplay(
:)
- webview.load()

// set global variable for checking selected index tab
var prevTab: Int = 0
var currentTab: Int = 0;

class RootViewController: UIViewController, WKUIDelegate, UITabBarControllerDelegate {

// declare var for WebKit
var webView: WKWebView!

// load view of WebKit
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}

// for display URL in WebKit
func goToUrl(url: String){
let myURL = URL(string:url)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
class HomeViewController: RootViewController {

let defaultUrl : String = "https://yourdomain.com/"

override func viewDidLoad() {
super.viewDidLoad()

// for checking click on active tab to root page of tab
self.tabBarController?.delegate = self

// call URL to dipslayed on WebKit View
self.goToUrl(url: defaultUrl)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// Tells the delegate that the user selected an item in the tab bar
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController){

// get selected index tab
currentTab = self.tabBarController?.selectedIndex ?? 0

if(prevTab == currentTab){
// MUST GO TO DEFAULT URL
}

// save selected index tab for next checking
prevTab = currentTab
}

}

我希望当用户再次按下 ACTIVE 选项卡栏(prevTab 等于 currentTab)时,WKWebview 会转到默认 URL,但它会失败(什么也没有发生)。

最佳答案

根据到目前为止发布的问题,我了解您需要使用 . UITabBarController、UITabBarControllerDelegate

1) ```override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.tabBarController?.delegate = self
} ```

2) ```override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
} ```

3) ``` override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { switch item.title! {
case "First":
print("First")
case "Second":
print("Second")
default:
print("Home")
self.selectedIndex = 2 . /// selection of Tabbar item
}
} ```

关于swift - 当按下事件标签栏时如何加载 WKWebview 转到根页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55448579/

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