title: TabBar to view and when closed come back to previous selected view
body: <p>I'm developing an iOS app with swift in which I have a TabBarController with 5 tab bar items. All of them points to a navigation controller and then to a view controller. One of them I want to show a view controller without the tab bar and when the user press cancel it should go back to the previous tab bar item/view that was selected (previously - sorry for the redundancy). They are all linked/referenced by a "Relationship "view controllers" to "name of the view", but I don't have any specific segue or whatsoever.</p>
  
  <p>This is the code for that specific "button" which I call in the <strong>viewDidLoad</strong> function:</p>
  
  <pre><code>func setupMiddleButton() {
      let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
      var menuButtonFrame = menuButton.frame
      menuButtonFrame.origin.y = self.view.bounds.height - menuButtonFrame.height
      menuButtonFrame.origin.x = self.view.bounds.width/2 - menuButtonFrame.size.width/2
      menuButton.frame = menuButtonFrame
  
      menuButton.backgroundColor = UIColor.white
      menuButton.layer.cornerRadius = menuButtonFrame.height/2
  
      menuButton.setImage(UIImage(named: "klein_fototoestel_2"), for: UIControlState.normal) // 450 x 450px
      menuButton.contentMode = .scaleAspectFit
      menuButton.addTarget(self, action: #selector(menuButtonAction), for: UIControlEvents.touchUpInside)
  
      self.view.addSubview(menuButton)
  
  
      self.view.layoutIfNeeded()
  }
  
  func menuButtonAction(sender: UIButton) {
      self.selectedIndex = 2
  }
  </code></pre>
  
  <p>I tried to perform the behaviour I want by delegating the tab bar controller with the following code <strong>but</strong> this function is never called when the central button is selected (though the correct view shows up..!):</p>
  
  <pre><code>override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
  
      print("the selected index is : \(tabBar.items?.index(of: item))")
  }
  </code></pre>
  
  <p>What I really want to know is what is the correct way to implement that behaviour I want. Remembering that all views have a navigationController before. I read a lot of people suggesting using UserDefaults to store the index of the previous controller but to be honest I really don't think that's appropriate.</p>
  
  <p>Any help is appreciated. </p>
  
  <p>Thanks in advance.</p>