Swift multiple subviews and getting back to original TableView












1















I have created my own TabView the first tab is always the Home tab which contains a TableView . The other 3 Tabs Search, Menu and Inbox are subviews . I can go from




  • Home to Search then Back to Home and it works

  • Home to Menu then Back to Home and it works too

  • Home to Menu then to Search and back to Home brings me back to the Menu subview . I essentially want to eliminate all subviews when clicking the Home Tab . Also each TabView is in it's own controller .


This is my code



From Home Controller to Menu Controller



@IBAction func MenuTabAction(_ sender: UIButton) {
let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MenuC") as! MenuC

self.addChildViewController(Popup)
Popup.view.frame = self.view.frame
Popup.view.tag = 100
self.view.addSubview(Popup.view)
Popup.didMove(toParentViewController: self)

}


From Menu Controller to Home Controller & Search Controller



   @IBAction func HomeTabAction(_ sender: UIButton) {
if let viewWithTag = self.view.viewWithTag(100) {
print("Tag 100")
viewWithTag.removeFromSuperview()
}

}

@IBAction func SearchTabAction(_ sender: UIButton) {

let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LocalSearchC") as! LocalSearchC
Popup.view.frame = self.view.frame
Popup.view.tag = 100
self.view.addSubview(Popup.view)
Popup.didMove(toParentViewController: self)
}


I am guessing that the remove superview only removes 1 superview at a time so if I go from Subview1 to subview2 then click on the HomeTab it brings me to subview1 instead of the original HomeTab . Is there a way to remove all superview/subviews when clicking the Home Tab ?



enter image description here










share|improve this question





























    1















    I have created my own TabView the first tab is always the Home tab which contains a TableView . The other 3 Tabs Search, Menu and Inbox are subviews . I can go from




    • Home to Search then Back to Home and it works

    • Home to Menu then Back to Home and it works too

    • Home to Menu then to Search and back to Home brings me back to the Menu subview . I essentially want to eliminate all subviews when clicking the Home Tab . Also each TabView is in it's own controller .


    This is my code



    From Home Controller to Menu Controller



    @IBAction func MenuTabAction(_ sender: UIButton) {
    let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MenuC") as! MenuC

    self.addChildViewController(Popup)
    Popup.view.frame = self.view.frame
    Popup.view.tag = 100
    self.view.addSubview(Popup.view)
    Popup.didMove(toParentViewController: self)

    }


    From Menu Controller to Home Controller & Search Controller



       @IBAction func HomeTabAction(_ sender: UIButton) {
    if let viewWithTag = self.view.viewWithTag(100) {
    print("Tag 100")
    viewWithTag.removeFromSuperview()
    }

    }

    @IBAction func SearchTabAction(_ sender: UIButton) {

    let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LocalSearchC") as! LocalSearchC
    Popup.view.frame = self.view.frame
    Popup.view.tag = 100
    self.view.addSubview(Popup.view)
    Popup.didMove(toParentViewController: self)
    }


    I am guessing that the remove superview only removes 1 superview at a time so if I go from Subview1 to subview2 then click on the HomeTab it brings me to subview1 instead of the original HomeTab . Is there a way to remove all superview/subviews when clicking the Home Tab ?



    enter image description here










    share|improve this question



























      1












      1








      1








      I have created my own TabView the first tab is always the Home tab which contains a TableView . The other 3 Tabs Search, Menu and Inbox are subviews . I can go from




      • Home to Search then Back to Home and it works

      • Home to Menu then Back to Home and it works too

      • Home to Menu then to Search and back to Home brings me back to the Menu subview . I essentially want to eliminate all subviews when clicking the Home Tab . Also each TabView is in it's own controller .


      This is my code



      From Home Controller to Menu Controller



      @IBAction func MenuTabAction(_ sender: UIButton) {
      let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MenuC") as! MenuC

      self.addChildViewController(Popup)
      Popup.view.frame = self.view.frame
      Popup.view.tag = 100
      self.view.addSubview(Popup.view)
      Popup.didMove(toParentViewController: self)

      }


      From Menu Controller to Home Controller & Search Controller



         @IBAction func HomeTabAction(_ sender: UIButton) {
      if let viewWithTag = self.view.viewWithTag(100) {
      print("Tag 100")
      viewWithTag.removeFromSuperview()
      }

      }

      @IBAction func SearchTabAction(_ sender: UIButton) {

      let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LocalSearchC") as! LocalSearchC
      Popup.view.frame = self.view.frame
      Popup.view.tag = 100
      self.view.addSubview(Popup.view)
      Popup.didMove(toParentViewController: self)
      }


      I am guessing that the remove superview only removes 1 superview at a time so if I go from Subview1 to subview2 then click on the HomeTab it brings me to subview1 instead of the original HomeTab . Is there a way to remove all superview/subviews when clicking the Home Tab ?



      enter image description here










      share|improve this question
















      I have created my own TabView the first tab is always the Home tab which contains a TableView . The other 3 Tabs Search, Menu and Inbox are subviews . I can go from




      • Home to Search then Back to Home and it works

      • Home to Menu then Back to Home and it works too

      • Home to Menu then to Search and back to Home brings me back to the Menu subview . I essentially want to eliminate all subviews when clicking the Home Tab . Also each TabView is in it's own controller .


      This is my code



      From Home Controller to Menu Controller



      @IBAction func MenuTabAction(_ sender: UIButton) {
      let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MenuC") as! MenuC

      self.addChildViewController(Popup)
      Popup.view.frame = self.view.frame
      Popup.view.tag = 100
      self.view.addSubview(Popup.view)
      Popup.didMove(toParentViewController: self)

      }


      From Menu Controller to Home Controller & Search Controller



         @IBAction func HomeTabAction(_ sender: UIButton) {
      if let viewWithTag = self.view.viewWithTag(100) {
      print("Tag 100")
      viewWithTag.removeFromSuperview()
      }

      }

      @IBAction func SearchTabAction(_ sender: UIButton) {

      let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LocalSearchC") as! LocalSearchC
      Popup.view.frame = self.view.frame
      Popup.view.tag = 100
      self.view.addSubview(Popup.view)
      Popup.didMove(toParentViewController: self)
      }


      I am guessing that the remove superview only removes 1 superview at a time so if I go from Subview1 to subview2 then click on the HomeTab it brings me to subview1 instead of the original HomeTab . Is there a way to remove all superview/subviews when clicking the Home Tab ?



      enter image description here







      ios swift addsubview superview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 21:10









      rmaddy

      242k27316380




      242k27316380










      asked Nov 17 '18 at 19:38









      user1591668user1591668

      81821540




      81821540
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Each Tab got it's own view controller. Ideally, you should removeFromSuperview all controllers you're not showing.



          At your code, you only removeFromSuperView at HomeTabAction.



          Try to change it:



          if let viewWithTag = self.view.viewWithTag(100) {
          print("Tag 100")
          viewWithTag.removeFromSuperview()
          }


          to



          for v in self.view.subviews {
          if v.tag == 100 {
          v.removeFromSuperview()
          }
          }


          But please, keep in mind that each time a user press any tabs without returning to home (i.e: tapping many times between Menu and Search), it's look like you are just instantiating many controllers, without removing them.



          You should remove other Views every time a new one is instantiated. Would be wise to give a unique tag to each view controller and remove the hidden others after every change, not only when returning to Home. Or at least, check if the view controller with a given type already is instantiated before create a new one.






          share|improve this answer

































            1














            Actually, you don't need to manually instantiate the viewcontrollers (LocalSearch, Menu). TabViewcontrollers can link a vc with each tab item via a segue. In fact, when you add your tabvc to the project, it will come with 2 viewcontrollers, each connected to an item in the tabview, and that's it, you just need to replace them or adapt them, no need to "load" them.



            The only scenario where you'd need to do this, is if your buttons were "dynamic", as in, the content to be loaded changes depending on some other circumstances. As long as clicking "Search" goes to LocalSearchViewController, just link it with a segue on the storyboard.






            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',
              autoActivateHeartbeat: false,
              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%2f53354851%2fswift-multiple-subviews-and-getting-back-to-original-tableview%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Each Tab got it's own view controller. Ideally, you should removeFromSuperview all controllers you're not showing.



              At your code, you only removeFromSuperView at HomeTabAction.



              Try to change it:



              if let viewWithTag = self.view.viewWithTag(100) {
              print("Tag 100")
              viewWithTag.removeFromSuperview()
              }


              to



              for v in self.view.subviews {
              if v.tag == 100 {
              v.removeFromSuperview()
              }
              }


              But please, keep in mind that each time a user press any tabs without returning to home (i.e: tapping many times between Menu and Search), it's look like you are just instantiating many controllers, without removing them.



              You should remove other Views every time a new one is instantiated. Would be wise to give a unique tag to each view controller and remove the hidden others after every change, not only when returning to Home. Or at least, check if the view controller with a given type already is instantiated before create a new one.






              share|improve this answer






























                1














                Each Tab got it's own view controller. Ideally, you should removeFromSuperview all controllers you're not showing.



                At your code, you only removeFromSuperView at HomeTabAction.



                Try to change it:



                if let viewWithTag = self.view.viewWithTag(100) {
                print("Tag 100")
                viewWithTag.removeFromSuperview()
                }


                to



                for v in self.view.subviews {
                if v.tag == 100 {
                v.removeFromSuperview()
                }
                }


                But please, keep in mind that each time a user press any tabs without returning to home (i.e: tapping many times between Menu and Search), it's look like you are just instantiating many controllers, without removing them.



                You should remove other Views every time a new one is instantiated. Would be wise to give a unique tag to each view controller and remove the hidden others after every change, not only when returning to Home. Or at least, check if the view controller with a given type already is instantiated before create a new one.






                share|improve this answer




























                  1












                  1








                  1







                  Each Tab got it's own view controller. Ideally, you should removeFromSuperview all controllers you're not showing.



                  At your code, you only removeFromSuperView at HomeTabAction.



                  Try to change it:



                  if let viewWithTag = self.view.viewWithTag(100) {
                  print("Tag 100")
                  viewWithTag.removeFromSuperview()
                  }


                  to



                  for v in self.view.subviews {
                  if v.tag == 100 {
                  v.removeFromSuperview()
                  }
                  }


                  But please, keep in mind that each time a user press any tabs without returning to home (i.e: tapping many times between Menu and Search), it's look like you are just instantiating many controllers, without removing them.



                  You should remove other Views every time a new one is instantiated. Would be wise to give a unique tag to each view controller and remove the hidden others after every change, not only when returning to Home. Or at least, check if the view controller with a given type already is instantiated before create a new one.






                  share|improve this answer















                  Each Tab got it's own view controller. Ideally, you should removeFromSuperview all controllers you're not showing.



                  At your code, you only removeFromSuperView at HomeTabAction.



                  Try to change it:



                  if let viewWithTag = self.view.viewWithTag(100) {
                  print("Tag 100")
                  viewWithTag.removeFromSuperview()
                  }


                  to



                  for v in self.view.subviews {
                  if v.tag == 100 {
                  v.removeFromSuperview()
                  }
                  }


                  But please, keep in mind that each time a user press any tabs without returning to home (i.e: tapping many times between Menu and Search), it's look like you are just instantiating many controllers, without removing them.



                  You should remove other Views every time a new one is instantiated. Would be wise to give a unique tag to each view controller and remove the hidden others after every change, not only when returning to Home. Or at least, check if the view controller with a given type already is instantiated before create a new one.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 17 '18 at 19:59

























                  answered Nov 17 '18 at 19:46









                  luizvluizv

                  175213




                  175213

























                      1














                      Actually, you don't need to manually instantiate the viewcontrollers (LocalSearch, Menu). TabViewcontrollers can link a vc with each tab item via a segue. In fact, when you add your tabvc to the project, it will come with 2 viewcontrollers, each connected to an item in the tabview, and that's it, you just need to replace them or adapt them, no need to "load" them.



                      The only scenario where you'd need to do this, is if your buttons were "dynamic", as in, the content to be loaded changes depending on some other circumstances. As long as clicking "Search" goes to LocalSearchViewController, just link it with a segue on the storyboard.






                      share|improve this answer




























                        1














                        Actually, you don't need to manually instantiate the viewcontrollers (LocalSearch, Menu). TabViewcontrollers can link a vc with each tab item via a segue. In fact, when you add your tabvc to the project, it will come with 2 viewcontrollers, each connected to an item in the tabview, and that's it, you just need to replace them or adapt them, no need to "load" them.



                        The only scenario where you'd need to do this, is if your buttons were "dynamic", as in, the content to be loaded changes depending on some other circumstances. As long as clicking "Search" goes to LocalSearchViewController, just link it with a segue on the storyboard.






                        share|improve this answer


























                          1












                          1








                          1







                          Actually, you don't need to manually instantiate the viewcontrollers (LocalSearch, Menu). TabViewcontrollers can link a vc with each tab item via a segue. In fact, when you add your tabvc to the project, it will come with 2 viewcontrollers, each connected to an item in the tabview, and that's it, you just need to replace them or adapt them, no need to "load" them.



                          The only scenario where you'd need to do this, is if your buttons were "dynamic", as in, the content to be loaded changes depending on some other circumstances. As long as clicking "Search" goes to LocalSearchViewController, just link it with a segue on the storyboard.






                          share|improve this answer













                          Actually, you don't need to manually instantiate the viewcontrollers (LocalSearch, Menu). TabViewcontrollers can link a vc with each tab item via a segue. In fact, when you add your tabvc to the project, it will come with 2 viewcontrollers, each connected to an item in the tabview, and that's it, you just need to replace them or adapt them, no need to "load" them.



                          The only scenario where you'd need to do this, is if your buttons were "dynamic", as in, the content to be loaded changes depending on some other circumstances. As long as clicking "Search" goes to LocalSearchViewController, just link it with a segue on the storyboard.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 17 '18 at 20:51









                          Sergio FloresSergio Flores

                          1126




                          1126






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354851%2fswift-multiple-subviews-and-getting-back-to-original-tableview%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







                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()