22

The object inherits from NSObject.

Is there a method to create a copy of it as a new object?

3
  • 2
    Why are you trying to copy a UIButton? There may be a better path... Jul 7, 2009 at 15:37
  • 3
    Is there a downside to copying a UIButton? I need to dynamically create new buttons based off of an existing one. Jul 9, 2009 at 17:30
  • You duplicate a button to make, say, a row of buttons. @JamesSkidmore Here's full code stackoverflow.com/a/26438692/294884
    – Fattie
    Oct 18, 2014 at 10:24

6 Answers 6

65

UIButton does not conform to NSCopying, so you cannot make a copy via -copy.

However, it does conform to NSCoding, so you can archive the current instance, then unarchive a 'copy'.

NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: button];
UIButton *buttonCopy = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];

Afterwards, you'll have to assign any additional properties that weren't carried over in the archive (e.g. the delegate) as necessary.

2
  • 6
    Clever! I would've probably just added -copy via a category, but this is better. =) Jul 7, 2009 at 15:46
  • 3
    Isn't this a bit slow?
    – mattsven
    Aug 2, 2013 at 15:22
4

UIButton doesn't conform to the NSCopying protocol, so you have copy it by hand. On the other hand, it is not a bad thing, since it is not exactly clear what does it mean to copy a button. For example, should it add the button copy to the same view the original is in? Should it fire the same methods when tapped?

3

To add to Jim's answer above using a category

 @implementation UIButton (NSCopying)

 - (id)copyWithZone:(NSZone *)zone {
     NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:self];
     UIButton *buttonCopy = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
     return buttonCopy;
 }

 @end

if you wanted to copy all of the actions from one button to another, add something like this:

 for (id target in button.allTargets) {
    NSArray *actions = [button actionsForTarget:target forControlEvent:UIControlEventTouchUpInside];
    for (NSString *action in actions) {
        [newButton addTarget:target action:NSSelectorFromString(action) forControlEvents:UIControlEventTouchUpInside];
    }
 }
0

If it implements the NSCopying protocol, then the -copy method should do the trick.

1
  • I'm trying to copy a UIButton. Any ideas? Jul 7, 2009 at 15:15
0

You can get more information about the -copy method and how it works with sub-objects on the ADC reference site. As Stephen Darlington mentions, you need to implement the NSCopying protocol in your object.

documentation

0

Swift 3/4 version would be:

let archivedData = NSKeyedArchiver.archivedData(withRootObject: button as Any)
let buttonCopy = NSKeyedUnarchiver.unarchiveObject(with: archivedData) as? UIButton

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.