How to duplicate a UIButton in Objective C?

时间:2015-01-30 15:37:44   收藏:0   阅读:131

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];
    }
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!