Duplicate the UIButton and Move it

http://stackoverflow.com/questions/19241208/duplicate-the-uibutton-and-move-it/26438692#26438692

I have one UIButton(lets say instance1).I have set target method when it moves.So when I try to move(drag) I want to do that create copy of instance1 and that copy button should be moved(dragged).Yes I know that -copy will not support to UIButton.So I used :

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

But the problem is when I am trying to move copy button,its not moving and on second attempt it is hiding/removing from screen.Please help me.

share|improve this question
 
                                                                                                                    
try to create a "copy constructor". create a new button with alloc init, then set it's properties one by one after the source button.                 –                      Calin Chitu                 Oct 8 '13 at 7:23                                                                                                 
                                                                                                                    
Try to make a custom buttom class with copyWithZone and use it...                 –                      Javier Peigneux                 Oct 8 '13 at 7:30                                                                            
                                                                                                                    
you should also show the code you use to move it around                 –                      micantox                 Oct 8 '13 at 8:02                                                                            
                                                                                                                    
@JavierPeigneux : to use copyWithZone , i need to create subclass of UIButton.Can we make subclass of UIButton?                 –                      h999                 Oct 8 '13 at 9:56                                                                            
                                                                                                                    
@h999 Of course, you can. I don't know if it is the best way but it is an option.                 –                      Javier Peigneux                 Oct 8 '13 at 13:20                                                                            

1 Answer                                 1

Your example button is self.exampleButton...

-(UIButton*)_copie
    {
    NSData *arch = [NSKeyedArchiver archivedDataWithRootObject: self.exampleButton];
    UIButton *nu = [NSKeyedUnarchiver unarchiveObjectWithData: arch];
    [self.view addSubview:nu];
    nu.frame = self.exampleButton.frame;
    [nu addTarget:self
        action:@selector(oneLinkClicked:)
        forControlEvents:UIControlEventTouchUpInside];
    return nu;
    }

here's an example of making a number of the buttons, in a vertical column.

in the example the data comes from a Dictionary...

CGPoint zero = self.exampleButton.center;
CGFloat gap = self.exampleButton.bounds.size.height * 1.25;

NSInteger kount=0;
self.orderedLinks = [[NSMutableArray alloc] init];

for ( NSDictionary *link in self.arrayOfLinks )
    {
    NSLog( @"one title... %@", link[@"title"] );
    NSLog( @"one url...   %@", link[@"url"] );

    UIButton *copy = [self _copie];

    CGPoint newpos = zero;
    newpos.y = newpos.y + ( kount * gap );
    copy.center = newpos;

    [copy setTitle:link[@"title"] forState:UIControlStateNormal];
    copy.tag = kount;

    [self.orderedLinks addObject:link[@"url"]];

    kount++;
    }

[self.exampleButton removeFromSuperview];

and when a button is clicked...

-(IBAction)oneLinkClicked:(UIButton *)sender
    {
    NSLog(@"this tag clicked ...... %ld", (long)sender.tag);
    NSString *goUrl = self.orderedLinks[ sender.tag ];

    NSLog(@"goUrl ...... %@", goUrl );
    }
share|improve this answer

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

导航