zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Entity Framework4.0 (一)概述(EF4 的Database First方法)
    开园第一篇,Hello cnBlog
    汇编学习笔记32
    汇编学习笔记25
    汇编学习笔记33
    汇编学习笔记28
    汇编学习笔记29,30
    汇编学习笔记23
    汇编学习笔记27
    汇编学习笔记31
  • 原文地址:https://www.cnblogs.com/xuejinhui/p/4262201.html
Copyright © 2011-2022 走看看