0

I am having a problem! When I use addSubview: and add a view to a UIScrollView, it adds a completely new view.

I want to either:

  • toggle between 3 predefined views, or
  • remove the last view from the UIScrollView before adding another one.

(All 3 views have some images so I'd imagine that the second option would be more efficient, memory-wise. However, perhaps the first option would be better on battery life? This is a 'subquestion'.)

How does one accomplish this?

Luke
  • 11,310
  • 43
  • 59
  • 67
Derek
  • 35
  • 3

2 Answers2

1

You could accomplish your plan B by using removeFromSuperview function of your UIView.

Use it as below.

[mySecondImageView removeFromSuperview];
[myScrollView addSubview:myThirdImageView];

EDITED:

When you create UIImageView assign a tag value to each and also use and integer iVar to hold the tag value of your current view lets say it's currentViewTag ;

myFirstImageView.tag = 1;
mySecondImageView.tag = 2;
myThirdImageView.tag = 3;

currentViewTag = 1;
[myScrollView addSubview:myFirstImageView];

Now use as below

[[myScrollView viewWithTag:currentViewTag] removeFromSuperview];
[myScrollView addSubview:myThirdImageView];
currentViewTag = 3;
Jhaliya - Praveen Sharma
  • 31,294
  • 8
  • 69
  • 74
  • Thanks Jhaliya. Is there a way to, instead of using "mySecondImageView", get the current view of UIScroll view? E.g.: something like [[myScrollView currentView] removeFromSuperview]? – Derek Jun 22 '11 at 10:23
  • Thanks so much, Jhaliya. This is exactly what I wanted! – Derek Jun 22 '11 at 11:49
  • Update: [[myScrollView viewWithTag:currentViewTag] removeFromSuperview] was giving me problems. It wouldn't let any other views load for some reason (I even checked to make sure the tags and currentTagView numbers where correct), so I used another method I found from http://stackoverflow.com/questions/1340754/does-removefromsuperview-releases-the-objects-of-scrollview – Derek Jun 22 '11 at 15:39
1

Sounds to me that you are trying to implement an infinite scrolling uiscrollview like the photos app.

If so, you don't need to remove any subviews and add them, you just need to create a UIScrollView that is as wide as three of your images and then when the scroll view is scrolled, change the image in either position 1 or 3 depending on which way the scroll is performed and then reset the position of the scroll view. If this is what you are trying to do, I can give more information if you need it.

Nick Bull
  • 4,244
  • 1
  • 16
  • 25
  • Thanks for the suggestion, but what I'm really making is just 3 buttons that, when you click each of them, changes the UIScrollView to another view (that has some images inside of the view (though the focus of the view is not on the images)). – Derek Jun 22 '11 at 15:32