一个有 6 个标签的 TabBar,一个 ViewController,一个 WebView

A TabBar with 6 tabs, A ViewController, A WebView

我是 swift 的初学者。 我想创建 Web 视图应用程序优化导航(查看商业网站)。

我决定使用 TabBarController(有 6 个标签)和 UIWebView。我可以实现这个应用程序。

  1. TabBarController 相关 6 ViewControllers
  2. 6 ViewControllers 有每个 UIWebView(指定的 URI)

我可以看到 6 个选项卡,例如网站。 但我有疑问...

如果可能的话,我想使用带有 6 个选项卡的 TabBarController 和一个 ViewController 以及一个 UIWebView。 当我可以 select 一个选项卡(在 6 个选项卡中)时,我想更改相同 UIWebView.

的 url

请告诉我如何解决这个问题。 提前致谢。

我认为,在 TabBarController 中不可能有一个 ViewController,但你可以有一个 WebView 并在你的视图控制器之间共享它。

  1. 以单例样式创建 WebView:

    class WebView: UIWebView
    {
        static let sharedInstance = WebView()
    }
    
  2. 将其置于视图中,将出现在屏幕上:

    class FirstViewController: UIViewController
    {
        override func viewWillAppear(animated: Bool)
        {
            view.addSubview(WebView.sharedInstance)
            WebView.sharedInstance.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
    
           // configure it as you need: load request, setup autolayout constraints, etc..
        }
    }
    
    class SecondViewController: UIViewController
    {   
        override func viewWillAppear(animated: Bool)
        {
            view.addSubview(WebView.sharedInstance)
            WebView.sharedInstance.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
    
            // same
        }
    }
    
    // and same in each view controller