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. 十、mysql之索引原理与慢查询优化

    mysql之索引原理与慢查询优化 一.介绍 1.什么是索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还 ...

  2. iOS 引用外部静态库(.a文件)时或打包.a时,Category方法无法调用。崩溃

    我的这个是MJRefresh,学习打.a包Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ...

  3. iOS-QQ好友列表实现

    0.QQ好友列表实现 0.首先说说实现思路 自定义UITableView,每一个分组都是一个UITableViewHeaderFooterView,然后自定义cell,这里分组的实现主要是自定义UIT ...

  4. 抓包工具 - Fiddler - (一)

    <转载于 miantest> Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的,使用的代理地址是:127.0.0.1,端口默认为8888 ...

  5. (原)Unreal渲染模块 管线 - 程序和场景查询

    @author: 白袍小道 查看随意,转载随缘     第一部分: 这里主要关心加速算法,和该阶段相关的UE模块的结构和组件的处理. What-HOW-Why-HOW-What(嘿嘿,老规矩) 1.渲 ...

  6. 【LeetCode】Swap Nodes in Pairs(两两交换链表中的节点)

    这是LeetCode里的第24题. 题目要求: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定1->2->3->4, 你应该返回2->1->4- ...

  7. MySQL数据库有哪些特点

    MySQL数据库的特点有: 它是C和C++语言编写的.支持多个操作系统.支持多线程.为多种编程语言提供API.优化SQL算法提高了查询速度以及提供用于管理和检查数据库的管理工具 MySQL数据库 My ...

  8. shell之进程

    ps     System V 风格  -         -elF         -ef         -eF     BSD            a所有跟终端有关的进程           ...

  9. sql的over函数的使用

    over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用.其参数:over(partition by columnname1 order by c ...

  10. KindleEditor insertfile初始化多个

    KindEditor.ready(function (K) { var editor = K.editor({ allowFileManager: true }); K('#insertfile'). ...