How to reset tab (or tab controller) of UITabBarController when user select tab?











up vote
1
down vote

favorite












Swift: I have UITabBarController, with 8 tabs. When user select any tab including more tab, I want reset content of selected tab by Popping to rootView controller?



How could this be done?



I tried to reset navigation controllers in below methods, It works for tabs which are visible at bottom but it doesn't work for More tab.



tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) 

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)









share|improve this question
























  • what have you tried already? show some code, show what errors your getting or explain it in more detail
    – Scriptable
    Apr 28 '17 at 10:37












  • @Scriptable I tried to reset navigation controllers in method tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
    – Gaurav Borole
    Apr 28 '17 at 10:41

















up vote
1
down vote

favorite












Swift: I have UITabBarController, with 8 tabs. When user select any tab including more tab, I want reset content of selected tab by Popping to rootView controller?



How could this be done?



I tried to reset navigation controllers in below methods, It works for tabs which are visible at bottom but it doesn't work for More tab.



tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) 

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)









share|improve this question
























  • what have you tried already? show some code, show what errors your getting or explain it in more detail
    – Scriptable
    Apr 28 '17 at 10:37












  • @Scriptable I tried to reset navigation controllers in method tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
    – Gaurav Borole
    Apr 28 '17 at 10:41















up vote
1
down vote

favorite









up vote
1
down vote

favorite











Swift: I have UITabBarController, with 8 tabs. When user select any tab including more tab, I want reset content of selected tab by Popping to rootView controller?



How could this be done?



I tried to reset navigation controllers in below methods, It works for tabs which are visible at bottom but it doesn't work for More tab.



tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) 

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)









share|improve this question















Swift: I have UITabBarController, with 8 tabs. When user select any tab including more tab, I want reset content of selected tab by Popping to rootView controller?



How could this be done?



I tried to reset navigation controllers in below methods, It works for tabs which are visible at bottom but it doesn't work for More tab.



tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) 

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)






ios swift xcode uitabbarcontroller uitabbar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 1 at 9:51









Krunal

35.6k20130156




35.6k20130156










asked Apr 28 '17 at 10:32









Gaurav Borole

3721624




3721624












  • what have you tried already? show some code, show what errors your getting or explain it in more detail
    – Scriptable
    Apr 28 '17 at 10:37












  • @Scriptable I tried to reset navigation controllers in method tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
    – Gaurav Borole
    Apr 28 '17 at 10:41




















  • what have you tried already? show some code, show what errors your getting or explain it in more detail
    – Scriptable
    Apr 28 '17 at 10:37












  • @Scriptable I tried to reset navigation controllers in method tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
    – Gaurav Borole
    Apr 28 '17 at 10:41


















what have you tried already? show some code, show what errors your getting or explain it in more detail
– Scriptable
Apr 28 '17 at 10:37






what have you tried already? show some code, show what errors your getting or explain it in more detail
– Scriptable
Apr 28 '17 at 10:37














@Scriptable I tried to reset navigation controllers in method tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
– Gaurav Borole
Apr 28 '17 at 10:41






@Scriptable I tried to reset navigation controllers in method tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
– Gaurav Borole
Apr 28 '17 at 10:41














4 Answers
4






active

oldest

votes

















up vote
3
down vote



accepted










A clean way to do this:



func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let index = tabBarController.selectedIndex
if index == NSNotFound || index > 4 {
tabBarController.moreNavigationController.popToRootViewController(animated: false)
return
}
let navController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController
navController?.popToRootViewController(animated: false)
}


Create a custom class for your UITabViewContoller, set a delegate and put there that piece of code.






share|improve this answer























  • kamil3 It works for tabs which are visible at bottom, but doesn't work for More Tab :(
    – Gaurav Borole
    Apr 28 '17 at 10:58










  • @GauravBorole Generally Apple dissuades using more than 5 tabs in tab bar: developer.apple.com/ios/human-interface-guidelines/ui-bars/…. Of course that doesn't change the fact that you're right.
    – kamil3
    Apr 28 '17 at 11:05










  • kamil3 Yes Thats true, But thats the situation :)
    – Gaurav Borole
    Apr 28 '17 at 11:06










  • @GauravBorole I edited my answer - it turns out UITabBarController has moreNavigationController property. Does it meet your requirements?
    – kamil3
    Apr 28 '17 at 11:40










  • kamil3 Thanks It works for more tab as well. But I think for MoreTab we need to add check if it is MoreTab index then Pop to root view.
    – Gaurav Borole
    Apr 28 '17 at 11:43


















up vote
0
down vote













You can create a UITabBarController class for your tabBar and in that class ovveride the tabBar didSelect method from their on select of any tab you can popViewController



 class TabBarViewController: UITabBarController {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let navigationController1 = self.viewControllers![0] as? UINavigationController
navigationController1!.popViewController(animated: false)

let navigationController2 = self.viewControllers![1] as? UINavigationController
navigationController2!.popToRootViewController(animated: false)


let navigationController3 = self.viewControllers![2] as? UINavigationController
navigationController3!.popToRootViewController(animated: false)

let navigationController4 = self.viewControllers![3] as? UINavigationController
navigationController4!.popToRootViewController(animated: false)

}
}





share|improve this answer





















  • It works for tabs which are visible at bottom, but doesn't work for More Tab :(
    – Gaurav Borole
    Apr 28 '17 at 10:59










  • add a screenshot of your storyboard in the question
    – harshal jadhav
    Apr 28 '17 at 11:00










  • ViewControllers are programmatically added to UITabBarController. But I guess it doesn't matter wether you are using Storyboard and Programatic way.
    – Gaurav Borole
    Apr 28 '17 at 11:05


















up vote
0
down vote













Use viewControllers property of UITabbarController. viewControlers is an array of UIViewController for UITabbarController. Exchange/Move position of view controllers upon tabbar(controller) selection.



Use tabbar(controller)'s delegate method to hanlde these operations, like:

Swift 3



func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if var viewcontrollers = tabBarController.viewControllers {
viewcontrollers.remove(at: tabBarController.selectedIndex)
viewcontrollers.insert(viewController, at: 0)
tabBarController.viewControllers = viewControllers
}
}


You can also use this delegate method, if you have an access to tabbarController where you have implemented.



func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
// Write a similar code to exchange/move view controllers using tabbarController instance.
}





share|improve this answer






























    up vote
    0
    down vote













    This worked for me.



    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    guard let navigationController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController else {
    return
    }

    navigationController.popToRootViewController(animated: false)
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43678076%2fhow-to-reset-tab-or-tab-controller-of-uitabbarcontroller-when-user-select-tab%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      A clean way to do this:



      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      let index = tabBarController.selectedIndex
      if index == NSNotFound || index > 4 {
      tabBarController.moreNavigationController.popToRootViewController(animated: false)
      return
      }
      let navController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController
      navController?.popToRootViewController(animated: false)
      }


      Create a custom class for your UITabViewContoller, set a delegate and put there that piece of code.






      share|improve this answer























      • kamil3 It works for tabs which are visible at bottom, but doesn't work for More Tab :(
        – Gaurav Borole
        Apr 28 '17 at 10:58










      • @GauravBorole Generally Apple dissuades using more than 5 tabs in tab bar: developer.apple.com/ios/human-interface-guidelines/ui-bars/…. Of course that doesn't change the fact that you're right.
        – kamil3
        Apr 28 '17 at 11:05










      • kamil3 Yes Thats true, But thats the situation :)
        – Gaurav Borole
        Apr 28 '17 at 11:06










      • @GauravBorole I edited my answer - it turns out UITabBarController has moreNavigationController property. Does it meet your requirements?
        – kamil3
        Apr 28 '17 at 11:40










      • kamil3 Thanks It works for more tab as well. But I think for MoreTab we need to add check if it is MoreTab index then Pop to root view.
        – Gaurav Borole
        Apr 28 '17 at 11:43















      up vote
      3
      down vote



      accepted










      A clean way to do this:



      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      let index = tabBarController.selectedIndex
      if index == NSNotFound || index > 4 {
      tabBarController.moreNavigationController.popToRootViewController(animated: false)
      return
      }
      let navController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController
      navController?.popToRootViewController(animated: false)
      }


      Create a custom class for your UITabViewContoller, set a delegate and put there that piece of code.






      share|improve this answer























      • kamil3 It works for tabs which are visible at bottom, but doesn't work for More Tab :(
        – Gaurav Borole
        Apr 28 '17 at 10:58










      • @GauravBorole Generally Apple dissuades using more than 5 tabs in tab bar: developer.apple.com/ios/human-interface-guidelines/ui-bars/…. Of course that doesn't change the fact that you're right.
        – kamil3
        Apr 28 '17 at 11:05










      • kamil3 Yes Thats true, But thats the situation :)
        – Gaurav Borole
        Apr 28 '17 at 11:06










      • @GauravBorole I edited my answer - it turns out UITabBarController has moreNavigationController property. Does it meet your requirements?
        – kamil3
        Apr 28 '17 at 11:40










      • kamil3 Thanks It works for more tab as well. But I think for MoreTab we need to add check if it is MoreTab index then Pop to root view.
        – Gaurav Borole
        Apr 28 '17 at 11:43













      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      A clean way to do this:



      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      let index = tabBarController.selectedIndex
      if index == NSNotFound || index > 4 {
      tabBarController.moreNavigationController.popToRootViewController(animated: false)
      return
      }
      let navController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController
      navController?.popToRootViewController(animated: false)
      }


      Create a custom class for your UITabViewContoller, set a delegate and put there that piece of code.






      share|improve this answer














      A clean way to do this:



      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      let index = tabBarController.selectedIndex
      if index == NSNotFound || index > 4 {
      tabBarController.moreNavigationController.popToRootViewController(animated: false)
      return
      }
      let navController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController
      navController?.popToRootViewController(animated: false)
      }


      Create a custom class for your UITabViewContoller, set a delegate and put there that piece of code.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 28 '17 at 12:46

























      answered Apr 28 '17 at 10:51









      kamil3

      4821713




      4821713












      • kamil3 It works for tabs which are visible at bottom, but doesn't work for More Tab :(
        – Gaurav Borole
        Apr 28 '17 at 10:58










      • @GauravBorole Generally Apple dissuades using more than 5 tabs in tab bar: developer.apple.com/ios/human-interface-guidelines/ui-bars/…. Of course that doesn't change the fact that you're right.
        – kamil3
        Apr 28 '17 at 11:05










      • kamil3 Yes Thats true, But thats the situation :)
        – Gaurav Borole
        Apr 28 '17 at 11:06










      • @GauravBorole I edited my answer - it turns out UITabBarController has moreNavigationController property. Does it meet your requirements?
        – kamil3
        Apr 28 '17 at 11:40










      • kamil3 Thanks It works for more tab as well. But I think for MoreTab we need to add check if it is MoreTab index then Pop to root view.
        – Gaurav Borole
        Apr 28 '17 at 11:43


















      • kamil3 It works for tabs which are visible at bottom, but doesn't work for More Tab :(
        – Gaurav Borole
        Apr 28 '17 at 10:58










      • @GauravBorole Generally Apple dissuades using more than 5 tabs in tab bar: developer.apple.com/ios/human-interface-guidelines/ui-bars/…. Of course that doesn't change the fact that you're right.
        – kamil3
        Apr 28 '17 at 11:05










      • kamil3 Yes Thats true, But thats the situation :)
        – Gaurav Borole
        Apr 28 '17 at 11:06










      • @GauravBorole I edited my answer - it turns out UITabBarController has moreNavigationController property. Does it meet your requirements?
        – kamil3
        Apr 28 '17 at 11:40










      • kamil3 Thanks It works for more tab as well. But I think for MoreTab we need to add check if it is MoreTab index then Pop to root view.
        – Gaurav Borole
        Apr 28 '17 at 11:43
















      kamil3 It works for tabs which are visible at bottom, but doesn't work for More Tab :(
      – Gaurav Borole
      Apr 28 '17 at 10:58




      kamil3 It works for tabs which are visible at bottom, but doesn't work for More Tab :(
      – Gaurav Borole
      Apr 28 '17 at 10:58












      @GauravBorole Generally Apple dissuades using more than 5 tabs in tab bar: developer.apple.com/ios/human-interface-guidelines/ui-bars/…. Of course that doesn't change the fact that you're right.
      – kamil3
      Apr 28 '17 at 11:05




      @GauravBorole Generally Apple dissuades using more than 5 tabs in tab bar: developer.apple.com/ios/human-interface-guidelines/ui-bars/…. Of course that doesn't change the fact that you're right.
      – kamil3
      Apr 28 '17 at 11:05












      kamil3 Yes Thats true, But thats the situation :)
      – Gaurav Borole
      Apr 28 '17 at 11:06




      kamil3 Yes Thats true, But thats the situation :)
      – Gaurav Borole
      Apr 28 '17 at 11:06












      @GauravBorole I edited my answer - it turns out UITabBarController has moreNavigationController property. Does it meet your requirements?
      – kamil3
      Apr 28 '17 at 11:40




      @GauravBorole I edited my answer - it turns out UITabBarController has moreNavigationController property. Does it meet your requirements?
      – kamil3
      Apr 28 '17 at 11:40












      kamil3 Thanks It works for more tab as well. But I think for MoreTab we need to add check if it is MoreTab index then Pop to root view.
      – Gaurav Borole
      Apr 28 '17 at 11:43




      kamil3 Thanks It works for more tab as well. But I think for MoreTab we need to add check if it is MoreTab index then Pop to root view.
      – Gaurav Borole
      Apr 28 '17 at 11:43












      up vote
      0
      down vote













      You can create a UITabBarController class for your tabBar and in that class ovveride the tabBar didSelect method from their on select of any tab you can popViewController



       class TabBarViewController: UITabBarController {
      override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
      let navigationController1 = self.viewControllers![0] as? UINavigationController
      navigationController1!.popViewController(animated: false)

      let navigationController2 = self.viewControllers![1] as? UINavigationController
      navigationController2!.popToRootViewController(animated: false)


      let navigationController3 = self.viewControllers![2] as? UINavigationController
      navigationController3!.popToRootViewController(animated: false)

      let navigationController4 = self.viewControllers![3] as? UINavigationController
      navigationController4!.popToRootViewController(animated: false)

      }
      }





      share|improve this answer





















      • It works for tabs which are visible at bottom, but doesn't work for More Tab :(
        – Gaurav Borole
        Apr 28 '17 at 10:59










      • add a screenshot of your storyboard in the question
        – harshal jadhav
        Apr 28 '17 at 11:00










      • ViewControllers are programmatically added to UITabBarController. But I guess it doesn't matter wether you are using Storyboard and Programatic way.
        – Gaurav Borole
        Apr 28 '17 at 11:05















      up vote
      0
      down vote













      You can create a UITabBarController class for your tabBar and in that class ovveride the tabBar didSelect method from their on select of any tab you can popViewController



       class TabBarViewController: UITabBarController {
      override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
      let navigationController1 = self.viewControllers![0] as? UINavigationController
      navigationController1!.popViewController(animated: false)

      let navigationController2 = self.viewControllers![1] as? UINavigationController
      navigationController2!.popToRootViewController(animated: false)


      let navigationController3 = self.viewControllers![2] as? UINavigationController
      navigationController3!.popToRootViewController(animated: false)

      let navigationController4 = self.viewControllers![3] as? UINavigationController
      navigationController4!.popToRootViewController(animated: false)

      }
      }





      share|improve this answer





















      • It works for tabs which are visible at bottom, but doesn't work for More Tab :(
        – Gaurav Borole
        Apr 28 '17 at 10:59










      • add a screenshot of your storyboard in the question
        – harshal jadhav
        Apr 28 '17 at 11:00










      • ViewControllers are programmatically added to UITabBarController. But I guess it doesn't matter wether you are using Storyboard and Programatic way.
        – Gaurav Borole
        Apr 28 '17 at 11:05













      up vote
      0
      down vote










      up vote
      0
      down vote









      You can create a UITabBarController class for your tabBar and in that class ovveride the tabBar didSelect method from their on select of any tab you can popViewController



       class TabBarViewController: UITabBarController {
      override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
      let navigationController1 = self.viewControllers![0] as? UINavigationController
      navigationController1!.popViewController(animated: false)

      let navigationController2 = self.viewControllers![1] as? UINavigationController
      navigationController2!.popToRootViewController(animated: false)


      let navigationController3 = self.viewControllers![2] as? UINavigationController
      navigationController3!.popToRootViewController(animated: false)

      let navigationController4 = self.viewControllers![3] as? UINavigationController
      navigationController4!.popToRootViewController(animated: false)

      }
      }





      share|improve this answer












      You can create a UITabBarController class for your tabBar and in that class ovveride the tabBar didSelect method from their on select of any tab you can popViewController



       class TabBarViewController: UITabBarController {
      override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
      let navigationController1 = self.viewControllers![0] as? UINavigationController
      navigationController1!.popViewController(animated: false)

      let navigationController2 = self.viewControllers![1] as? UINavigationController
      navigationController2!.popToRootViewController(animated: false)


      let navigationController3 = self.viewControllers![2] as? UINavigationController
      navigationController3!.popToRootViewController(animated: false)

      let navigationController4 = self.viewControllers![3] as? UINavigationController
      navigationController4!.popToRootViewController(animated: false)

      }
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 28 '17 at 10:44









      harshal jadhav

      2,6352822




      2,6352822












      • It works for tabs which are visible at bottom, but doesn't work for More Tab :(
        – Gaurav Borole
        Apr 28 '17 at 10:59










      • add a screenshot of your storyboard in the question
        – harshal jadhav
        Apr 28 '17 at 11:00










      • ViewControllers are programmatically added to UITabBarController. But I guess it doesn't matter wether you are using Storyboard and Programatic way.
        – Gaurav Borole
        Apr 28 '17 at 11:05


















      • It works for tabs which are visible at bottom, but doesn't work for More Tab :(
        – Gaurav Borole
        Apr 28 '17 at 10:59










      • add a screenshot of your storyboard in the question
        – harshal jadhav
        Apr 28 '17 at 11:00










      • ViewControllers are programmatically added to UITabBarController. But I guess it doesn't matter wether you are using Storyboard and Programatic way.
        – Gaurav Borole
        Apr 28 '17 at 11:05
















      It works for tabs which are visible at bottom, but doesn't work for More Tab :(
      – Gaurav Borole
      Apr 28 '17 at 10:59




      It works for tabs which are visible at bottom, but doesn't work for More Tab :(
      – Gaurav Borole
      Apr 28 '17 at 10:59












      add a screenshot of your storyboard in the question
      – harshal jadhav
      Apr 28 '17 at 11:00




      add a screenshot of your storyboard in the question
      – harshal jadhav
      Apr 28 '17 at 11:00












      ViewControllers are programmatically added to UITabBarController. But I guess it doesn't matter wether you are using Storyboard and Programatic way.
      – Gaurav Borole
      Apr 28 '17 at 11:05




      ViewControllers are programmatically added to UITabBarController. But I guess it doesn't matter wether you are using Storyboard and Programatic way.
      – Gaurav Borole
      Apr 28 '17 at 11:05










      up vote
      0
      down vote













      Use viewControllers property of UITabbarController. viewControlers is an array of UIViewController for UITabbarController. Exchange/Move position of view controllers upon tabbar(controller) selection.



      Use tabbar(controller)'s delegate method to hanlde these operations, like:

      Swift 3



      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      if var viewcontrollers = tabBarController.viewControllers {
      viewcontrollers.remove(at: tabBarController.selectedIndex)
      viewcontrollers.insert(viewController, at: 0)
      tabBarController.viewControllers = viewControllers
      }
      }


      You can also use this delegate method, if you have an access to tabbarController where you have implemented.



      func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
      // Write a similar code to exchange/move view controllers using tabbarController instance.
      }





      share|improve this answer



























        up vote
        0
        down vote













        Use viewControllers property of UITabbarController. viewControlers is an array of UIViewController for UITabbarController. Exchange/Move position of view controllers upon tabbar(controller) selection.



        Use tabbar(controller)'s delegate method to hanlde these operations, like:

        Swift 3



        func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if var viewcontrollers = tabBarController.viewControllers {
        viewcontrollers.remove(at: tabBarController.selectedIndex)
        viewcontrollers.insert(viewController, at: 0)
        tabBarController.viewControllers = viewControllers
        }
        }


        You can also use this delegate method, if you have an access to tabbarController where you have implemented.



        func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        // Write a similar code to exchange/move view controllers using tabbarController instance.
        }





        share|improve this answer

























          up vote
          0
          down vote










          up vote
          0
          down vote









          Use viewControllers property of UITabbarController. viewControlers is an array of UIViewController for UITabbarController. Exchange/Move position of view controllers upon tabbar(controller) selection.



          Use tabbar(controller)'s delegate method to hanlde these operations, like:

          Swift 3



          func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
          if var viewcontrollers = tabBarController.viewControllers {
          viewcontrollers.remove(at: tabBarController.selectedIndex)
          viewcontrollers.insert(viewController, at: 0)
          tabBarController.viewControllers = viewControllers
          }
          }


          You can also use this delegate method, if you have an access to tabbarController where you have implemented.



          func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
          // Write a similar code to exchange/move view controllers using tabbarController instance.
          }





          share|improve this answer














          Use viewControllers property of UITabbarController. viewControlers is an array of UIViewController for UITabbarController. Exchange/Move position of view controllers upon tabbar(controller) selection.



          Use tabbar(controller)'s delegate method to hanlde these operations, like:

          Swift 3



          func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
          if var viewcontrollers = tabBarController.viewControllers {
          viewcontrollers.remove(at: tabBarController.selectedIndex)
          viewcontrollers.insert(viewController, at: 0)
          tabBarController.viewControllers = viewControllers
          }
          }


          You can also use this delegate method, if you have an access to tabbarController where you have implemented.



          func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
          // Write a similar code to exchange/move view controllers using tabbarController instance.
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 1 at 9:50

























          answered Apr 28 '17 at 10:47









          Krunal

          35.6k20130156




          35.6k20130156






















              up vote
              0
              down vote













              This worked for me.



              func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
              guard let navigationController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController else {
              return
              }

              navigationController.popToRootViewController(animated: false)
              }





              share|improve this answer

























                up vote
                0
                down vote













                This worked for me.



                func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
                guard let navigationController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController else {
                return
                }

                navigationController.popToRootViewController(animated: false)
                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  This worked for me.



                  func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
                  guard let navigationController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController else {
                  return
                  }

                  navigationController.popToRootViewController(animated: false)
                  }





                  share|improve this answer












                  This worked for me.



                  func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
                  guard let navigationController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController else {
                  return
                  }

                  navigationController.popToRootViewController(animated: false)
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 21:27









                  Simon Mo

                  62839




                  62839






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43678076%2fhow-to-reset-tab-or-tab-controller-of-uitabbarcontroller-when-user-select-tab%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine