Can i reuse UIView? - iphone

I have UIView in my storyboard with outlet mynewView and i want it display with totally different content. And do it many times.
My UIView contains many objects, i load it from internet. But every time i do
NSArray *viewsToRemove = [view1 mynewView];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
//Let's try to remove all possible views..
viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
//here i create mynewView
mynewView = [self createnewRandomView];
//And finally add the View we have created.
[self.view addSubview:mynewView];
i monitor the memory consumption and i see it grows every time i add new UIView. Even when i remove all superviews. I guess they remain in memory.
Before i change the view i remove from superview all possible views, but they still stay in memory.
Xcode is set to use ARC so i can't release it.
Is there any good way to reuse a UIView?
Thanks

[self.view addSubview:mynewView];
Your newView was added as subview of self.view.
Your above code does not use for remove yourVewView in self.view.
Please try this code before adding new view:
for (UIView *view in self.view) {
if([view isKindOfClass:[YourNewViewClassName class]])
{
[view removeFromSuperView];
break;
}
}

As you remove the subviews, set them to nil as you go, like so:
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
v = nil;
}
For more information, see for example Arc: Setting references to nil, for multiple buttons

Related

How to remove a subView from detailView?

I have a table in my masterView and in my detailView i have some imageView, based on different row selection in my masterView i have to remove ImageView and add some other views. Im unable to remove the ImageView.... any help ??? thanks in advance
try:
for (UIView *view in masterView.subviews){
if (view == yourImageView) {
[view removeFromSuperview];
}
}
first you provide tag values other than 0 to all your imageview
and then
for (UIView *sub in [self.view subviews]) {
use tag to find proper view and remove it from superView
}

Removing superview in objective-c

I'm looking through an example in the iPhone Beginning programming book and they have code to switch between two views when a button is pressed. Here's the first snippet from their example code:
if (self.yellowViewController.view.superview == nil)
{
if (self.yellowViewController == nil)
{
YellowViewController *yellowController =
[[YellowViewController alloc] initWithNibName:#"YellowView"
bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
}
else
{
if (self.blueViewController == nil)
{
BlueViewController *blueController =
[[BlueViewController alloc] initWithNibName:#"BlueView"
bundle:nil];
self.blueViewController = blueController;
[blueController release];
}
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
}
It does make sense to me, but the question I have is, how would you do this with a UISegmentControl that has four views. I know you can check for the selectedSegment and create that view when needed. But how would I know what the last view was in order to remove it from the superview beforing adding my new view as a subview? Thanks!
while creating each view either code or IB set tag value to segmentIndex.so u can get them later by that tag value.this is tricky and simple.
You could check to see which view is allocated or not nil and then remove.
if (yellowController) {
[yellowController.view removeFromSuperView];
[yellowController release];
}
You could go through your four views to determine which one is loaded and then remove the view.
For any UIView the frontmost subview is [[myView subviews] lastObject].

Change subview on UIView ? [close]

i have success show subview from another NIB, i have 2 button and 1 UIView on MainView, when i klick button 1 will show NIB1 on UIView, and then i klick button 2 will show NIB2 on UIView, but when i klick again button 1 UIView not show NIB1 again, but still NIB 2, this is my Code :
Updated Solved
On .h file
IBOutlet UIView *bottomView;
On .m file
-(IBAction) handleButton1{
for (UIView *t in bottomView.subviews) {
[t removeFromSuperview];
}
if (![nib1 isViewLoaded]) {
nib1 = [[NIB1 alloc] initWithNibName:#"NIB1" bundle:nil];
}
[bottomView addSubview:nib1.view];
}
-(IBAction) handleButton2{
for (UIView *t in bottomView.subviews) {
[t removeFromSuperview];
}
if (![nib2 isViewLoaded]) {
nib2 = [[NIB2 alloc] initWithNibName:#"NIB2" bundle:nil];
}
[bottomView addSubview:nib2.view];
}
How i can refresh UIView with another NIB on my code ?
Thanks,
I have solve this case, i remove all subview on MainView from this http://stackoverflow.com/questions/3915729/refreshing-cell-content-after-deleting-cells
use :
for (UIView *t in bottomView.subviews) {
[t removeFromSuperview];
}
I hope this code help anyone :).
You have to remove the first view view [theViewYouDontWantToShow removeFromSuperview].
Or you can use [bottomView bringSubviewToFront:nib1/2.view].
What was appending here is only that you added a view (this works), added the other view on top of the first one (this works too), but then you added the first one again. The view doesn't change since it's already there.

add subview loaded from a nib into a frame

I want to add a view from a nib file as a subview of my main view at the click of a button, but it has to be in a frame (0,108,320,351). I've tried the following code:
-(IBAction) displayJoinmeetup:(id)sender
{
[meetups removeFromSuperview]; //removing the older view
CGRect myFrame = CGRectMake(0, 108,320,351);
[joinmeetup initWithFrame:myFrame]; //joinmeetup declared in header file as IBoutlet UIView*joinmeetup
[self.view addSubview:joinmeetup];
}
This displays only a blank screen, otherwise if I remove the frame the subview displays covering the whole screen, how can I properly do this?
You should never call init... on an object after it has been created. If you can change a setting, there will be a setter for it. In this case, it is setFrame:
-(IBAction) displayJoinmeetup:(id)sender {
[meetups removeFromSuperview]; //removing the older view
CGRect myFrame = CGRectMake(0, 108,320,351);
[joinmeetup setFrame:myFrame]; //joinmeetup declared in header file as IBoutlet UIView*joinmeetup
[self.view addSubview:joinmeetup];
}
initWithFrame is not called for a view loaded from nib. See the UIView class ref for details.
You need to set the view size properties in interface builder (inspector).
You can try thi as follow:
-(IBAction) displayJoinmeetup:(id)sender
{
if(meetups)
{
[meetups removeFromSuperview]; //removing the older view
//also here you should release the meetups, if it is allocated
meetups = nil;
}
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 108,320,351)];
joinmeetup = tempView;
[tempView release];
[self.view addSubview:joinmeetup];
}
I suggest you try this:
[joinmeetup setFrame:CGRectMake(0, 108,320,351)];
[self.view addSubview:joinmeetup];

Removing UIScrollView subviews assistance

I'd like some assistance to properly remove subviews from a scrollview, to reduce memory usage. Every time the user scrolls, I ask to grab page, page -1 and page+1 to get the views loaded in the scrollview.
Now my issue is that if there's ten views side by side, when you reach the tenth view, all the previous 9 views are still subviews in the scrollview and in memory. I'd like to reverse load these views as well. Here's what I have so far:
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [self favouritesCount]) return;
// replace the placeholder if necessary
MyObject *myObj = (MyObj *)[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:page inSection:0]];
MyView *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyView alloc] initWithObject:myObj];
controller.delegate = self;
[viewControllers replaceObjectAtIndex:page withObject:controller];
}
// add the controller's view to the scroll view
if (nil == controller.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.frame = frame;
[scrollView addSubview:controller];
}
}
Now I have tried something like:
if (page > 2) {
[(UIView *)[[scrollView subviews] objectAtIndex:(page - 2)] removeFromSuperview];
}
But that just crashes it. It's probably a simple solution, so help is appreciated :)
Did you increment the pageCount?
You can try with this as well:
MyView *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller != [NSNull null]) {
[controller removeFromSuperView];
}
Some notes for naming convention: MyView *controller does not make sense. If MyView is subclass of UIView, you should only name if MyView *view
I think if I were you, I would just add "loadData" and "releaseData" methods on my views, and call those when they're coming in and out of view. Sure, you'll still have the view classes loaded, but in those methods you can get rid of (and re-load) the more heavyweight sub-objects that those views contain.
Good luck,
Blake.
I think your problem is in the solution you posted:
[(UIView *)[[scrollView subviews] objectAtIndex:(page - 2)] removeFromSuperview];
If you remove the previously loaded views, you always have 2 views loaded, so you cant access page-2 because you don't have page-2 views loaded in the scrollview. (think for example when page is 5, you will access view 3 from scrollview)
You should consider 2 things:
1.- Use [[[viewControllers objectAtIndex:page-2] view] removeFromSuperview]; instead (take a look that here I use viewcontrollers instead of scroll.subviews)
2.- Remove the view from the viewcontrollers array , because just removing the view from the scroll will not release the view from memory (its retained by the array). For this, use something like:
[viewControllers replaceObjectAtIndex:page-2 withObject:[NSNull null]];