您的位置:首页 > 产品设计 > UI/UE

Duplicate the UIButton and Move it

2015-01-30 13:56 155 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐