Duplicate the UIButton and Move it

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


1down votefavorite

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.

ios objective-c uibutton nsdata


share|improve this question

edited Oct 8 ‘13 at 7:13

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

activeoldestvotes


     up vote0down vote

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
时间: 2024-11-13 02:31:59

Duplicate the UIButton and Move it的相关文章

How to duplicate a UIButton in Objective C?

http://stackoverflow.com/questions/1092875/how-to-duplicate-a-uibutton-in-objective-c 1down vote To add to Jim's answer above using a category @implementation UIButton (NSCopying) - (id)copyWithZone:(NSZone *)zone { NSData *archivedData = [NSKeyedArc

JS Range HTML文档/文字内容选中、库及应用介绍

一.前面的些话 本文的内容基本上是基于"区域范围对象(Range objects)"这个概念来说的.这个玩意,可以让你选择HTML文档的任意部分,并可以拿这些选择的信息做你想做的事情.其中,最常见的Range是用户用鼠标选择的内容(user selection). 本文有不少篇幅就是讲如何将用户的这种选择转换为W3C Range或Microsoft Text Range对象. 二.什么是Range? 所谓"Range",是指HTML文档中任意一段内容.一个Range

重构之1.Duplicate Observed Data 复制被监视数据

场景: 如果业务层的内容被内嵌于界面层中,我们需要帮这分离出来 代码坏味道 MyFrame /** * * @author wumingkun * @version 1.0.0 * @Description */ package com.demo.refactor; import java.util.Observable; import java.util.Observer; /** * @author wumingkun * */ public class MyFrame { private

UILabel && UIButton

一.效果展示 1. 启动界面只有一个按钮 2. 点击按钮,显示文本信息 二. 分析 1. 两个控件UILabel && UIButton 2. 点击按钮触动方法设置文本信息 三. 实现 1. 不加载Main.storyboard 2. APPDelegate.m 1 // 2 // AppDelegate.m 3 // 4.1-标签和按钮 4 // 5 // Created by LinKun on 16/8/31. 6 // Copyright © 2016年 Lkun. All rig

成员函数指针的应用 之 仿写OC里面UIButton的回调机制(三)

// // main.cpp // Basic // // Created by 06 on 15/1/9. // Copyright (c) 2015年 黄永锐. All rights reserved. // #include <iostream> using namespace std; class UIButton; // struct NSObject{//负责内存管理的类 bool init(){ return true; } }; //英雄类 struct Hero:public

LeetCode-Cycle Detection,Find the Duplicate Number

      本来以为这题很简单,但是看要求,第一不能改变这个数组,第二只能使用O(1)的空间,第三时间复杂度小于O(n^2),就不能使用遍历数组的方式来解决了. 有两种方法,一种是利用Binary Search,一种是利用Floyd的cycle detection算法. Binary Search Method This method is based on a theroy called Pigeonhole princinpal. In mathematics,Pigeonhole prin

ios开发中UIButton的使用(一)

ios开发中UIButton的使用(一) 一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状态 normal(普通状态) 默认情况(Default) 对应的枚举常量:UIControlStateNormal highlighted(高亮状态) 按钮被按下去的时候(手指还未松开) 对应的枚举常量:UIControlStateHighlighted disabled(失效状态,不可用状态

iOS UI02.2_UIView,UILabel,UIButton,UItextfield归纳

// //  AppDelegate.m //  UI02作业 // //  Created by dllo on 15/7/30. //  Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import "AppDelegate.h" @interface AppDelegate () @property(nonatomic,retain)UITextField *numTextfield; @property(non

UIKit - UIButton 按钮操作

UIButton 按钮操作 /* 一 UIVIew 常见属性 1.frame 位置和尺寸(以父控件的左上角为原点(0,0)) 2.center 中点 (以父控件的左上角为原点(0,0)) 3.bounds 位置和尺寸(以自己的左上角为原点 (0,0)) 4.transform 形变属性(缩放,旋转) 5.backgroundColor 背景颜色 6.tag 标识(父控件可以根据这个标识找到对应的子控件,同一个父控件中的子控件不要一样) 7. hidden 设置是否要隐藏 8.alpha 透明度(