Question

Is there any easy way to copy an existing UI Control attribute from one to the other? Something like

*button1 = *button2;

I know that the above will not work, but just curious.

Yes, Yes, I know can use an XIB with a view with UIControl and create controls that way.

Was it helpful?

Solution

While there are ways to copy view hierarchies, they are hacky and should be avoided.

OTHER TIPS

NSKeyedArchiver & NSKeyedUnarchiver work well for me in most cases. I've never tried to clone a control with complicated structure though.

extension UIView {
    func deepCopy() -> UIView {

        let data = NSKeyedArchiver.archivedDataWithRootObject(self)
        let copy = NSKeyedUnarchiver.unarchiveObjectWithData(data)!  as! UIView

        return copy
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top