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. 18式优雅你的Python

    本文来自读者梁云同学的投稿,公众号:Python与算法之美 一,优雅你的Jupyter 1,更改Jupyter Notebook初始工作路径 平凡方法: 在cmd中输入jupyter notebook ...

  2. PHP.33-TP框架商城应用实例-后台9-商品相册-修改、删除(AJAX)

    商品相册图片删除 当商品删除时,把相册中的图片一并从硬盘和数据库中删除,根据商品id[因为每一张商品相片都会生成三张缩略图,所以删除时要将其缩略图一并删除] //钩子方法_before_delete: ...

  3. SharpCompress的压缩文件解压和文件夹压缩

    1.前言 最近做一个功能需要用到对压缩文件的解压,就找到了这个SharpCompress不错,还能解压rar的文件.但是网上的资料和我拿到的SharpCompress.dll的方法有些出入,所以我就自 ...

  4. ACE handle_timeout 事件重入

    当多线程运行反应器事件时, 注意handle_timeout会重入,单独线程不存在下列问题! 1. 一个timer事件 // test_ace_timer.cpp : Defines the entr ...

  5. Python 模块:random 随机数生成

    Python中的random模块用于生成随机数. 使用该模块之前需要 import random 几个常用的函数用法: 1.random.random 函数原型: random.random() 用于 ...

  6. 自动化测试(三)如何用python写个一函数,这个函数的功能是,传入一个数字,产生N条手机号,产生的手机号不能重复

    本期时间短没来得及写思路,不过我都加了注释,有问题可以@我 import randomimport timedef Phones(number):#生成手机号函数 s = "01234567 ...

  7. 百度webuploader上传 1

    百度webupload网址:http://fex.baidu.com/webuploader/ 引入js和css <script src="../../Content/webuploa ...

  8. linux sort的用法

    sort -n 表示按照数字 sort -k 表示第几列 sort -t : 表示按照:来分列 sort -r  表示从大到小排列

  9. SQL小助手——SQL Prompt

    背景: 当数据库设计的比较复杂.庞大时,我们如果对脚本不是很熟悉,就会很难完成看似简单的增.删.改.查的操作.我们需要一款软件来给出相应的提示或帮助,来提高代码的可读性,更快更好的完成任务. 简介: ...

  10. bpf 指令集

    58 struct bpf_insn { 59 __u8 code; /* opcode */ 60 __u8 dst_reg:4; /* dest register */ 61 __u8 src_r ...