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?
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)
ReplyDelete[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).
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.
ReplyDeleteIf 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