Skip to main content

What happens to my subView when changing navigation controllers?



Lets say that I add a subView to my view, like this:







[[self addSubview:myView];







Then I push a new view onto the navigationController stack, like this:







[self.navigationController pushViewController:otherView animated:YES];







What happens to the subView that I added to the original view. Is it removed automatically?





Also, if my program calls this line:







[[self addSubview:myView];







multiple times without removing the view does that do anything bad like create a memory leak?


Comments

  1. Firstly, be more careful about what you're talking. You don't add subviews to viewcontrollers, you add subview to views, i.e (self is a view controller)

    [self.view addSubView:myView];


    Also, you don't push views, but viewcontrollers, i.e

    [self.navigationController pushViewController:otherViewController animated:YES];


    Now with the first statement above, the view has a new subview. If you push a new view via navigationcontroller (second statement) over the original view, this does not really affect the original view. So, the subview you added will still be there after you pop the new view(controller).

    See it like that: Your first view controller has a view called View1. You added a subview to View1, fine. Now a push using UINavigationController pushes a new view controller over your old view controller, so you now see its view, View2. This has nothing to do with View1.

    To answer your second question, see add same subview multiple time to view - iphone

    Short answer, the view would be deleted from and instantly inserted into the parent view. So, while pointless, nothing bad would happen (the subview would just be pushed to the front).

    ReplyDelete
  2. Well, you could always use the test kit that comes with XCode to check for memory leaks, but I believe when you call the pushViewController it leaves the other view in tact. Also, if you're using the UINavigationController it automatically puts a nice back button for you to return to the previous view.

    ReplyDelete
  3. If you call [self.view addSubview: myView]; and then call [self.navigationController pushViewController: otherViewController animated:YES];, myView is not automatically removed from self.view. Calling [self.view addSubview: myView]; multiple times without removing myView, seems pointless, but you will not create a memory leak.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.