How to remove subviews in Objective-C?










21














I have added UIButton and UITextView as subviews to my view programmatically.



notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];

textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
[self.view addSubview:textView];
printf("n description button n");

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
addTarget:self action:@selector(cancel:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];


I need to remove all subviews when the button is clicked.



I have tried:



[self.view removeFromSuperView]


but it's not working.










share|improve this question




























    21














    I have added UIButton and UITextView as subviews to my view programmatically.



    notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
    notesDescriptionView.backgroundColor = [UIColor redColor];
    [self.view addSubview:notesDescriptionView];

    textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
    [self.view addSubview:textView];
    printf("n description button n");

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button
    addTarget:self action:@selector(cancel:)
    forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"OK" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
    [self.view addSubview:button];


    I need to remove all subviews when the button is clicked.



    I have tried:



    [self.view removeFromSuperView]


    but it's not working.










    share|improve this question


























      21












      21








      21


      6





      I have added UIButton and UITextView as subviews to my view programmatically.



      notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
      notesDescriptionView.backgroundColor = [UIColor redColor];
      [self.view addSubview:notesDescriptionView];

      textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
      [self.view addSubview:textView];
      printf("n description button n");

      button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [button
      addTarget:self action:@selector(cancel:)
      forControlEvents:UIControlEventTouchDown];
      [button setTitle:@"OK" forState:UIControlStateNormal];
      button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
      [self.view addSubview:button];


      I need to remove all subviews when the button is clicked.



      I have tried:



      [self.view removeFromSuperView]


      but it's not working.










      share|improve this question















      I have added UIButton and UITextView as subviews to my view programmatically.



      notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
      notesDescriptionView.backgroundColor = [UIColor redColor];
      [self.view addSubview:notesDescriptionView];

      textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
      [self.view addSubview:textView];
      printf("n description button n");

      button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [button
      addTarget:self action:@selector(cancel:)
      forControlEvents:UIControlEventTouchDown];
      [button setTitle:@"OK" forState:UIControlStateNormal];
      button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
      [self.view addSubview:button];


      I need to remove all subviews when the button is clicked.



      I have tried:



      [self.view removeFromSuperView]


      but it's not working.







      objective-c subview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 4:33









      Cœur

      17.5k9104145




      17.5k9104145










      asked Jun 9 '10 at 11:57









      macmac

      3,13072533




      3,13072533






















          3 Answers
          3






          active

          oldest

          votes


















          19














          I assume you're calling [self.view removeFromSuperView] from a method in the same class as the above snippet.



          In that case [self.view removeFromSuperView] removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:



          [notesDescriptionView removeFromSuperview];
          [button.view removeFromSuperview];
          [textView removeFromSuperview];


          Perhaps you'd want to store those subviews in an NSArray and loop over that array invoking removeFromSuperview on each element in that array.






          share|improve this answer






















          • what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
            – mac
            Jun 9 '10 at 12:28










          • i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
            – mac
            Jun 9 '10 at 12:37










          • What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
            – SanitLee
            Sep 26 '16 at 1:23


















          58














          to remove all the subviews you added to the view



          use the following code



          for (UIView *view in [self.view subviews]) 

          [view removeFromSuperview];






          share|improve this answer






























            7














            I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)



            Anyway, this is the little helper method that I use for that:



            - (void)removeAllSubviewsFromUIView:(UIView *)parentView

            for (id child in [parentView subviews])

            if ([child isMemberOfClass:[UIView class]])

            [child removeFromSuperview];






            EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?



            Am using that now as follows:



             // Make sure the background and foreground views are empty:
            [self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];


            I like that better.






            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%2f3005540%2fhow-to-remove-subviews-in-objective-c%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              19














              I assume you're calling [self.view removeFromSuperView] from a method in the same class as the above snippet.



              In that case [self.view removeFromSuperView] removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:



              [notesDescriptionView removeFromSuperview];
              [button.view removeFromSuperview];
              [textView removeFromSuperview];


              Perhaps you'd want to store those subviews in an NSArray and loop over that array invoking removeFromSuperview on each element in that array.






              share|improve this answer






















              • what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
                – mac
                Jun 9 '10 at 12:28










              • i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
                – mac
                Jun 9 '10 at 12:37










              • What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
                – SanitLee
                Sep 26 '16 at 1:23















              19














              I assume you're calling [self.view removeFromSuperView] from a method in the same class as the above snippet.



              In that case [self.view removeFromSuperView] removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:



              [notesDescriptionView removeFromSuperview];
              [button.view removeFromSuperview];
              [textView removeFromSuperview];


              Perhaps you'd want to store those subviews in an NSArray and loop over that array invoking removeFromSuperview on each element in that array.






              share|improve this answer






















              • what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
                – mac
                Jun 9 '10 at 12:28










              • i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
                – mac
                Jun 9 '10 at 12:37










              • What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
                – SanitLee
                Sep 26 '16 at 1:23













              19












              19








              19






              I assume you're calling [self.view removeFromSuperView] from a method in the same class as the above snippet.



              In that case [self.view removeFromSuperView] removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:



              [notesDescriptionView removeFromSuperview];
              [button.view removeFromSuperview];
              [textView removeFromSuperview];


              Perhaps you'd want to store those subviews in an NSArray and loop over that array invoking removeFromSuperview on each element in that array.






              share|improve this answer














              I assume you're calling [self.view removeFromSuperView] from a method in the same class as the above snippet.



              In that case [self.view removeFromSuperView] removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:



              [notesDescriptionView removeFromSuperview];
              [button.view removeFromSuperview];
              [textView removeFromSuperview];


              Perhaps you'd want to store those subviews in an NSArray and loop over that array invoking removeFromSuperview on each element in that array.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 21 '13 at 8:10

























              answered Jun 9 '10 at 12:05









              Frank SheararFrank Shearar

              15.1k65583




              15.1k65583











              • what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
                – mac
                Jun 9 '10 at 12:28










              • i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
                – mac
                Jun 9 '10 at 12:37










              • What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
                – SanitLee
                Sep 26 '16 at 1:23
















              • what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
                – mac
                Jun 9 '10 at 12:28










              • i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
                – mac
                Jun 9 '10 at 12:37










              • What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
                – SanitLee
                Sep 26 '16 at 1:23















              what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
              – mac
              Jun 9 '10 at 12:28




              what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
              – mac
              Jun 9 '10 at 12:28












              i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
              – mac
              Jun 9 '10 at 12:37




              i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
              – mac
              Jun 9 '10 at 12:37












              What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
              – SanitLee
              Sep 26 '16 at 1:23




              What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
              – SanitLee
              Sep 26 '16 at 1:23













              58














              to remove all the subviews you added to the view



              use the following code



              for (UIView *view in [self.view subviews]) 

              [view removeFromSuperview];






              share|improve this answer



























                58














                to remove all the subviews you added to the view



                use the following code



                for (UIView *view in [self.view subviews]) 

                [view removeFromSuperview];






                share|improve this answer

























                  58












                  58








                  58






                  to remove all the subviews you added to the view



                  use the following code



                  for (UIView *view in [self.view subviews]) 

                  [view removeFromSuperview];






                  share|improve this answer














                  to remove all the subviews you added to the view



                  use the following code



                  for (UIView *view in [self.view subviews]) 

                  [view removeFromSuperview];







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 19 '18 at 15:13









                  yoAlex5

                  3,39812421




                  3,39812421










                  answered Jun 9 '10 at 12:49









                  macmac

                  3,13072533




                  3,13072533





















                      7














                      I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)



                      Anyway, this is the little helper method that I use for that:



                      - (void)removeAllSubviewsFromUIView:(UIView *)parentView

                      for (id child in [parentView subviews])

                      if ([child isMemberOfClass:[UIView class]])

                      [child removeFromSuperview];






                      EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?



                      Am using that now as follows:



                       // Make sure the background and foreground views are empty:
                      [self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                      [self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];


                      I like that better.






                      share|improve this answer



























                        7














                        I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)



                        Anyway, this is the little helper method that I use for that:



                        - (void)removeAllSubviewsFromUIView:(UIView *)parentView

                        for (id child in [parentView subviews])

                        if ([child isMemberOfClass:[UIView class]])

                        [child removeFromSuperview];






                        EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?



                        Am using that now as follows:



                         // Make sure the background and foreground views are empty:
                        [self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                        [self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];


                        I like that better.






                        share|improve this answer

























                          7












                          7








                          7






                          I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)



                          Anyway, this is the little helper method that I use for that:



                          - (void)removeAllSubviewsFromUIView:(UIView *)parentView

                          for (id child in [parentView subviews])

                          if ([child isMemberOfClass:[UIView class]])

                          [child removeFromSuperview];






                          EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?



                          Am using that now as follows:



                           // Make sure the background and foreground views are empty:
                          [self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                          [self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];


                          I like that better.






                          share|improve this answer














                          I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)



                          Anyway, this is the little helper method that I use for that:



                          - (void)removeAllSubviewsFromUIView:(UIView *)parentView

                          for (id child in [parentView subviews])

                          if ([child isMemberOfClass:[UIView class]])

                          [child removeFromSuperview];






                          EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?



                          Am using that now as follows:



                           // Make sure the background and foreground views are empty:
                          [self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                          [self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];


                          I like that better.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 12 '18 at 4:36









                          Cœur

                          17.5k9104145




                          17.5k9104145










                          answered Feb 21 '14 at 6:07









                          Erik van der NeutErik van der Neut

                          1,84011618




                          1,84011618



























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f3005540%2fhow-to-remove-subviews-in-objective-c%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

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Syphilis

                              Darth Vader #20