3

I need to make a deep copy of a UIView containing several subviews. Please help me figure out how to do this? I've done some searching on google and SO and not found anything that does what I want.

Explanation for those who would like to know why: I have an infinitely scrolling UIScrollView where, of course, the contents repeat. Each 'set' of the contents (for example, say 26 UIButtons labeled A-Z) is contained inside one view. This is the view I need to make a deep copy of, so that I can display multiple instances of it onscreen within the scrollview.

I cannot simply recreate the structure of the view containing the buttons, because pressing the buttons calls a function of a class that my UIScrollView subclass does not have access to. Nor would I wish to recreate them, because it seems a waste to go through the logic to recreate and place all subviews in the container view when I could simply make a deep copy instead.

Can anyone help me?

WendiKidd
  • 4,333
  • 4
  • 33
  • 50
  • So every time you scroll, you're allocating new UIViews, even though they're exact copies of ones you already have in memory? – Evan Cordell May 08 '12 at 19:32
  • Aside from Evan's excellent point above, have you looked into delegates? – Luke May 08 '12 at 19:38
  • @Evan Cordell Yes...I based it off the Apple sample code I found here: http://developer.apple.com/library/ios/#samplecode/StreetScroller/Introduction/Intro.html It doesn't even work properly, because the subviews aren't deep copies and so they only appear on the screen once (ie. the app launches, the UIView is onscreen. I scroll, and when it comes time to add another (identical) UIView, it appears but the original UIView seems to *disappear* because the subviews are pointers to single objects). The whole thing is a mess, and I would love to be able to deep copy and fix it! – WendiKidd May 08 '12 at 19:40
  • @Luke I have used Apple's delegates before but have never created my own. How would you suggest I use delegates to solve this problem? Thanks! – WendiKidd May 08 '12 at 19:41
  • You might find the link in the answer here of great use - it helped me learn a lot about delegation and how to write your own custom methods and whatnot. http://stackoverflow.com/questions/6208361/uiview-needs-to-push-a-uiviewcontroller-delegation – Luke May 08 '12 at 19:46
  • @Luke: Ahh, I see where you're going with this. I should say, then--even if I could recreate the buttons and their view every time, it seems a waste of processing power to do so...There are going to be quite a lot of them and they require data to be parsed through to create them, and then they must be laid out upon their container view (which is the object I'd like to deep copy in the first place). It seems like a better solution to be able to deep copy it instead of going through a lot of logic to recreate them over and over again. What do you think? – WendiKidd May 08 '12 at 19:51
  • Naturally I don't know how CPU intensive your app currently is on the hardware you have... maybe give both options a stab if you have time and then decide from there. I mean, you know about all the components in your scroll view - surely you could loop through [theUIView subviews] array, test for the objects you want and then duplicate them? – Luke May 08 '12 at 19:55
  • @Luke Certainly, if I were aware of a way to deep copy UIButtons--for example, the addTarget:action:forControlEvents method is the only way I know of to set a target for a UIButton, and I am not aware of a way to *get* those arguments from an already created UIButton in order to pass them to the copy. Herein lies my problem--how to copy all elements from all the subviews into new copies to place in another containing view. Does that make sense? – WendiKidd May 08 '12 at 20:00
  • Aha right I see now. I think the only way would be to create more copies at runtime and hide them; else you'd have to iterate through all subviews, re-create all frames, colors etc... but as you say you'd then likely run into issues copying button targets etc - but there might be something useful to help accomplish that here: http://stackoverflow.com/questions/5182860/how-to-get-uibutton-target-action-and-control-events – Luke May 08 '12 at 20:35
  • @Luke: You, sir, are brilliant. Thank you! That was exactly what I needed. With the ability to extract the target from the buttons, I was able to loop through all the subviews of the view I wanted to copy and replicate it exactly. Thank you!! If you want to post this as the answer, I would be happy to accept! – WendiKidd May 09 '12 at 13:05
  • Consider it done :) Very happy to hear you got it sorted! – Luke May 09 '12 at 13:24

1 Answers1

3

(Posted from my comment on request)

Aha right I see now. I think the only way would be to create more copies at runtime and hide them; else you'd have to iterate through all subviews, re-create all frames, colors etc... but as you say you'd then likely run into issues copying button targets etc - but there might be something useful to help accomplish that here:

How to get UIButton Target, Action and Control events?

Community
  • 1
  • 1
Luke
  • 11,426
  • 43
  • 60
  • 69
  • 1
    This was exactly what I needed--with the ability to copy everything out of UIButton, I was able to recreate my view exactly. I wish Apple would come out with the ability to deep copy UI elements itself, but this works in the absence of that. Thanks! :) – WendiKidd May 09 '12 at 13:49