A YOKOTA
A YOKOTA

Reputation: 3

A TabBar with 6 tabs, A ViewController, A WebView

I'm begginer for swift. I want to create the web view application optimized navigation (to see commerce site).

I decide to use TabBarController(with 6 tabs) and UIWebView. And I could implement this application.

  1. TabBarController related to 6 ViewControllers
  2. 6 ViewControllers has each UIWebView(specified URI)

I could see 6 tabs like website. But I have question...

If possible, I want to use a TabBarController with 6 tab and a ViewController and a UIWebView. And when I could select a tab(in 6tabs), I want to change the url of the same UIWebView.

Please tell me how to solve this. Thanks in advance.

Upvotes: 0

Views: 1396

Answers (1)

ifau
ifau

Reputation: 2120

I think, it is not possible to have one ViewController in TabBarController, but you can have one WebView and share it between your view controllers.

  1. Create WebView in singleton style:

    class WebView: UIWebView
    {
        static let sharedInstance = WebView()
    }
    
  2. Place it on view, that will appear on screen:

    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
    

Upvotes: 1

Related Questions