Objective-C keep track of my view in subviews array











up vote
0
down vote

favorite












I have a question about memory management.
For example I have an iPhone application that uses multiply programmatically created views.
for example programmatically generated buttons.



    UIButton *myButton=[UIButton alloc] initWithFrame:...; //etc


then, normally we add this button to subviews array:



    [self.view addSubview:myButton];


then we releasing button.



    [myButton release]


When I need to remove this button how can I keep track on this button in subviews array?
I know I can do this using tag property but I think exists another way to keep connection with it.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a question about memory management.
    For example I have an iPhone application that uses multiply programmatically created views.
    for example programmatically generated buttons.



        UIButton *myButton=[UIButton alloc] initWithFrame:...; //etc


    then, normally we add this button to subviews array:



        [self.view addSubview:myButton];


    then we releasing button.



        [myButton release]


    When I need to remove this button how can I keep track on this button in subviews array?
    I know I can do this using tag property but I think exists another way to keep connection with it.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a question about memory management.
      For example I have an iPhone application that uses multiply programmatically created views.
      for example programmatically generated buttons.



          UIButton *myButton=[UIButton alloc] initWithFrame:...; //etc


      then, normally we add this button to subviews array:



          [self.view addSubview:myButton];


      then we releasing button.



          [myButton release]


      When I need to remove this button how can I keep track on this button in subviews array?
      I know I can do this using tag property but I think exists another way to keep connection with it.










      share|improve this question















      I have a question about memory management.
      For example I have an iPhone application that uses multiply programmatically created views.
      for example programmatically generated buttons.



          UIButton *myButton=[UIButton alloc] initWithFrame:...; //etc


      then, normally we add this button to subviews array:



          [self.view addSubview:myButton];


      then we releasing button.



          [myButton release]


      When I need to remove this button how can I keep track on this button in subviews array?
      I know I can do this using tag property but I think exists another way to keep connection with it.







      iphone objective-c uiview subview addsubview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 6:17









      Cœur

      17k9102140




      17k9102140










      asked Nov 25 '11 at 13:53









      Oleg

      67041434




      67041434
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can simply assign it to an instance variable:



          UIButton *myButton = ...;
          [self.view addSubView:myButton];
          myInstanceVariable = myButton;
          [myButton release];


          You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview]; it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.






          share|improve this answer





















          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09


















          up vote
          0
          down vote













          You can try to declare somewhere a retain property of UIButton* type, that can be assigned with pointer value to your button instance:



          @interface myclass
          @property (retain, nonatomic) UIButton *savedButton;
          @end

          @implementation myclass
          @synthesize savedButton;

          - (void) someMethod...
          {
          ...
          UIButton *myButton=[UIButton alloc] initWithFrame:...;
          [self.view addSubview:myButton];
          self.savedButton = myButton;
          [myButton release];
          ...
          }
          ...
          @end





          share|improve this answer





















          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09











          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%2f8270116%2fobjective-c-keep-track-of-my-view-in-subviews-array%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








          up vote
          0
          down vote



          accepted










          You can simply assign it to an instance variable:



          UIButton *myButton = ...;
          [self.view addSubView:myButton];
          myInstanceVariable = myButton;
          [myButton release];


          You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview]; it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.






          share|improve this answer





















          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09















          up vote
          0
          down vote



          accepted










          You can simply assign it to an instance variable:



          UIButton *myButton = ...;
          [self.view addSubView:myButton];
          myInstanceVariable = myButton;
          [myButton release];


          You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview]; it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.






          share|improve this answer





















          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can simply assign it to an instance variable:



          UIButton *myButton = ...;
          [self.view addSubView:myButton];
          myInstanceVariable = myButton;
          [myButton release];


          You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview]; it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.






          share|improve this answer












          You can simply assign it to an instance variable:



          UIButton *myButton = ...;
          [self.view addSubView:myButton];
          myInstanceVariable = myButton;
          [myButton release];


          You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview]; it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '11 at 13:57









          DarkDust

          75.8k12154195




          75.8k12154195












          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09


















          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09
















          Thank you for your help
          – Oleg
          Nov 25 '11 at 14:09




          Thank you for your help
          – Oleg
          Nov 25 '11 at 14:09












          up vote
          0
          down vote













          You can try to declare somewhere a retain property of UIButton* type, that can be assigned with pointer value to your button instance:



          @interface myclass
          @property (retain, nonatomic) UIButton *savedButton;
          @end

          @implementation myclass
          @synthesize savedButton;

          - (void) someMethod...
          {
          ...
          UIButton *myButton=[UIButton alloc] initWithFrame:...;
          [self.view addSubview:myButton];
          self.savedButton = myButton;
          [myButton release];
          ...
          }
          ...
          @end





          share|improve this answer





















          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09















          up vote
          0
          down vote













          You can try to declare somewhere a retain property of UIButton* type, that can be assigned with pointer value to your button instance:



          @interface myclass
          @property (retain, nonatomic) UIButton *savedButton;
          @end

          @implementation myclass
          @synthesize savedButton;

          - (void) someMethod...
          {
          ...
          UIButton *myButton=[UIButton alloc] initWithFrame:...;
          [self.view addSubview:myButton];
          self.savedButton = myButton;
          [myButton release];
          ...
          }
          ...
          @end





          share|improve this answer





















          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09













          up vote
          0
          down vote










          up vote
          0
          down vote









          You can try to declare somewhere a retain property of UIButton* type, that can be assigned with pointer value to your button instance:



          @interface myclass
          @property (retain, nonatomic) UIButton *savedButton;
          @end

          @implementation myclass
          @synthesize savedButton;

          - (void) someMethod...
          {
          ...
          UIButton *myButton=[UIButton alloc] initWithFrame:...;
          [self.view addSubview:myButton];
          self.savedButton = myButton;
          [myButton release];
          ...
          }
          ...
          @end





          share|improve this answer












          You can try to declare somewhere a retain property of UIButton* type, that can be assigned with pointer value to your button instance:



          @interface myclass
          @property (retain, nonatomic) UIButton *savedButton;
          @end

          @implementation myclass
          @synthesize savedButton;

          - (void) someMethod...
          {
          ...
          UIButton *myButton=[UIButton alloc] initWithFrame:...;
          [self.view addSubview:myButton];
          self.savedButton = myButton;
          [myButton release];
          ...
          }
          ...
          @end






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '11 at 14:00









          Denis

          5,83012225




          5,83012225












          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09


















          • Thank you for your help
            – Oleg
            Nov 25 '11 at 14:09
















          Thank you for your help
          – Oleg
          Nov 25 '11 at 14:09




          Thank you for your help
          – Oleg
          Nov 25 '11 at 14:09


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8270116%2fobjective-c-keep-track-of-my-view-in-subviews-array%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

          Full-time equivalent

          Bicuculline

          さくらももこ