How to duplicate a UIButton in Objective C?

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

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

posted on 2015-01-30 13:54  沉淀2014  阅读(155)  评论(0编辑  收藏  举报

导航