5

I want to create a copy of a UIView, and I dont want to use NSKeyedArchiver because I am frequently creating a copy of many views, and using NSKeyedArchiver was slow.

I heard about copy or initWithZone:, but Googling it found it is not good for UIView. I don't want a copy which points to the original view because any changes made to it will also make changes to the copied UIView.

Kermit
  • 33,827
  • 13
  • 85
  • 121
Karan Sehgal
  • 91
  • 1
  • 5
  • You cannot copy a view. If you want a "copy" you either have to do a deep copy with NSCoding (the keyed archiver stuff) or you have to implement some copiable data object that your view uses to set itself up and updates, then copy that out of one stock view and add it to another. But there's no way to just "copy" a view. – Jason Coco Jul 02 '12 at 04:39
  • Wait, when you say that you want a copy that points to the original do you mean that you want the same view in 2 different places? – Chance Hudson Jul 02 '12 at 04:41
  • i just wanted to save the state of a uiview at a particular moment to another uiview because i have implemented rotation,scaling and movement of uiviews with touch. So can you plz tell me how to copy the properties of a uiview to other if it cant be copied..thnx – Karan Sehgal Jul 02 '12 at 04:43
  • No i dont waqnt the copied uiview to point to the original one...i want it to be cloned – Karan Sehgal Jul 02 '12 at 04:44
  • http://stackoverflow.com/questions/4425939/can-uiview-be-copied – trumpetlicks Jul 02 '12 at 13:21
  • To copy it you'd have to implement a "deep copy", since with any not-trivial view the real stuff is the embedded views. But if you just want to "save the state" of the view you might be able to do that by recording the frame of the view and its component views, and whatever other parameters are of interest to you. – Hot Licks Apr 17 '13 at 01:19

2 Answers2

0

You can copy a view using:

TheView *copyOfView =  (TheView *) [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:origionalTheView]];
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
-4

Create a UIView Category which can help in copying the view. Something like this:

- (id) copyView {
     UIView *a = [[UIView alloc]init];
     a.frame = self.frame;
     //set all other properties of the UIView
     return a; //return [a autorelease] if not using ARC
}
m4n1c
  • 127
  • 8