首页 > 代码库 > How to duplicate a UIButton in Objective C?

How to duplicate a UIButton in Objective C?

http://stackoverflow.com/questions/1092875/how-to-duplicate-a-uibutton-in-objective-c

1down vote

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];    }

How to duplicate a UIButton in Objective C?