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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex