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.

NAZIK 3,20582764    
        asked Oct 8 '13 at 7:04    
h999 10011    
 
                                                                                                                    
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                                                                            
 | show 2 more comments            

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

Duplicate the UIButton and Move it的更多相关文章

  1. How to duplicate a UIButton in Objective C?

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

  2. PS网页设计教程XXIX——如何在PS中设计一个画廊布局

    作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...

  3. 『重构--改善既有代码的设计』读书笔记---Duplicate Observed Data

    当MVC出现的时候,极大的推动了Model与View分离的潮流.然而对于一些已存在的老系统或者没有维护好的系统,你都会看到当前存在大把的巨大类----将Model,View,Controller都写在 ...

  4. IOS(二)基本控件UIButton、简易动画、transform属性、UIImageView

    UIButton //1.设置UIButton 的左右移动 .center属性 获得 CGPoint 来修改x y //1.设置UIButton 的放大缩小 bounds属性 获得CGRect 然后通 ...

  5. react native中一次错误排查 Error:Error: Duplicate resources

    最近一直在使用react native中,遇到了很多的坑,同时也学习到了一些移动端的开发经验. 今天在做一个打包的测试时,遇到了一个问题,打包过程中报错“Error:Error: Duplicate ...

  6. 287. Find the Duplicate Number 找出数组中的重复数字

    [抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  7. Duplicate Manager Pro for Mac(重复文件查找工具)破解版安装

    1.软件简介    Duplicate Manager Pro 是 macOS 系统上一款重复文件查找工具,可以帮你在 Mac 电脑上查找出磁盘上面的重复文件,然后让你对这些重复文件进行判断并删除,使 ...

  8. Lintcode521-Remove Duplicate Numbers in Array-Easy

    Description Given an array of integers, remove the duplicate numbers in it. You should: Do it in pla ...

  9. swift 带有下划线的UIbutton

    import UIKit /// 带下划线的Button class UnderlineButton: JYBaseButton { /// 下划线高度 var underLineHeight:CGF ...

随机推荐

  1. Linux与BSD不同

    https://linux.cn/article-3186-1.html https://www.howtogeek.com/190773/htg-explains-whats-the-differe ...

  2. Git-Git协同与工作协同

    Git支持的协议 首先来看看数据交换需要使用的协议. Git提供了丰富的协议支持,包括:SSH.GIT.HTTP.HTTPS.FTP.FTPS.RSYNC及前面已经看到的本地协议等.各种不同协议的UR ...

  3. html_outputer.py

    coding=UTF-8 # HTML输出器 import sys class htmlOutputer(): def __init__(self): self.data = [] def colle ...

  4. Hive 压缩技术Data Compression

    Mapreducwe 执行流程 :input > map > shuffle > reduce > output 压缩执行时间,map 之后,压缩,数据存储在本地磁盘,减少磁盘 ...

  5. MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':ge

    数据库表里命名有这个字段,可怎么就是报错呢,大神的解释: 加上之后立马好用!!!

  6. TouTiao开源项目 分析笔记14 段子评论

    1.段子页面详情 1.1.先看看预览界面吧 左边的页面已经实现了,现在的目的就是要实现点击左侧的每一个item 然后跳转到右边相应的段子详情页面. 1.2.首先肯定有右侧这个活动==>JokeC ...

  7. 1864: [Zjoi2006]三色二叉树

    1864: [Zjoi2006]三色二叉树 链接 分析: 做得最智障的一题了... 首先中间输出两个数之间没空格(换行居然也过了...), 写了dp[i][0/1/2],后来知道其实dp[i][0/1 ...

  8. echart搭配时间轴进行展示 (本例展示的是多时间 多地区 多指标条件 )

    1:照常先来几张图 看效果  2:首先 看官方文档 我把echart官方的例子给扒下来并整理了得出如下效果 上 案例图和代码 效果图 : 代码: <style type="text/c ...

  9. shell编程——

    一.分支语句 语法:(多路分支) case word in patterm1) list A ;; pattern2) list B ;; patternN) list N ;; esac例子:cas ...

  10. Django 2.0官方文档中文 总索引

    Django 2.0官方文档中文 渣翻 总索引 翻译 2017年12月08日 11:19:1 官方原文: https://docs.djangoproject.com/en/2.0/ 当前翻译版本:  ...