Forrest Forrest - 4 months ago 28
iOS Question

Can UIView be copied?

Simply using this way

UIView* view2 = [view1 copy]; //view1 existed


This will cause simulator can not launch this app.

Try retain,

UIView* view2 = [view1 retain]; //view1 existed
//modify view2 frame etc


Any modifications to view2 will apply to view1, I understand that view2 share same memory with view1.

Why can't
UIView
be copied? What is the reason?

Answer

Your app probably crashes with something like:

 [UIView copyWithZone:]: unrecognized selector sent to instance 0x1c6280

The reason is that UIView does not implement the copying protocol, and therefore there is no copyWithZone selector in UIView.