Johnny Cheuk
Johnny Cheuk

Reputation: 237

How do I keep adding UIButton and set them with different action programmatically?

Assuming I have a container that stores a list of items. By adding these items, I have to add a single UIView for each. I want to make a delete button that allows the user to delete the item that they don't want. How can I keep adding these buttons and separate them with different actions? Like this button is for deleting item A, and that button is for deleting item B? P.S. This situation is not allow to use tableView, and I've already handled the view stacking part. If you need me to show any of the code, please feel free to ask.

Updated:

The code of adding the Item:

-(void)appendAttachmentRow:(AttachmentItem *)attachment
{
AttachmentRowView * attachmentRowView = [[AttachmentRowView alloc]init];

screenWidth = CGRectGetWidth(self.view.bounds);
screenHeight = CGRectGetHeight(self.view.bounds);

// Set up the view in a single attachment row

// Attachment row container
CGRect attachmentRowFrame = CGRectMake(0, yLastLocation, screenWidth, 50);
UIView *attachmentRow = [[UIView alloc]initWithFrame:attachmentRowFrame];

// Attachment name label
CGRect attachmentNameLabelFrame = CGRectMake(70, 20, screenWidth / 3, 15);
UILabel *attachmentNameLabel = [[UILabel alloc]initWithFrame:attachmentNameLabelFrame];

// Attachment thumbnail image
CGRect attachmentImageThumbnailFrame = CGRectMake(10, 0, 50, 50);
UIImageView *attachmentImageThumbnail = [[UIImageView alloc]initWithFrame:attachmentImageThumbnailFrame];

CGRect attachmentRemoveFrame = CGRectMake(screenWidth - 40, 10, 30, 30);
attachment.attachmentRemove = [[UIButton alloc]initWithFrame:attachmentRemoveFrame];
[attachment.attachmentRemove setImage:[UIImage imageNamed:@"removeAttachmentButton"] forState:UIControlStateNormal];

[attachment.attachmentRemove addTarget:self action:@selector(removeAttachment:) forControlEvents:UIControlStateNormal];


attachmentImageThumbnail.image = attachment.attachmentImage;
attachmentNameLabel.text = attachment.attachmentName;

attachmentRow.layer.borderColor = [UIColor lightGrayColor].CGColor;
attachmentRow.layer.borderWidth = 1.0f;

[attachmentRow addSubview: attachmentImageThumbnail];
[attachmentRow addSubview: attachmentNameLabel];
[attachmentRow addSubview: attachment.attachmentRemove];
[[self attachmentCellCellIt] addSubview: attachmentRow];
[attachmentArray addObject:attachment];

yLastLocation += 50;
[[self attachmentCellCellIt]setFrame:CGRectMake(0, 337, screenWidth, yLastLocation)];

Upvotes: 0

Views: 152

Answers (4)

Abhishek
Abhishek

Reputation: 368

You need to give tag to button after creating UIView for an attachment.

Keep the method name same and try to work with the tag value.

For ex :

button.tag = 1000; // while creating it.

In method you passed UIButton as parameter

Inside method body

NSInteger tag = button.tag

[array removeObjectAtIndex:tag];

Upvotes: 2

Ratul Sharker
Ratul Sharker

Reputation: 8021

Hope i understand your situation,

You want to set selector dynamically. Say you have following selector declarations.

-(void)onPressA:(id)sender{ ... }
-(void)onPressB:(id)sender{ ... }
-(void)onPressC:(id)sender{ ... }
-(void)onPressD:(id)sender{ ... }

Now need to take an NSArray or other storage to store them. Let save them in array. To do that, you need to convert them into NSString as following

NSArray *selectorArr = @[NSStringFromSelector(@selector(onPressA:)),
                         NSStringFromSelector(@selector(onPressB:)),
                         NSStringFromSelector(@selector(onPressC:)),
                         NSStringFromSelector(@selector(onPressD:))];

Now you can back & forth from NSString to SEL & SEL to NSString as following.

SEL selector = NSSelectorFromString(selectorArray[/*suitable index*/]);

Now you can easily add and remove target by using

[btn addTarget:/*target*/ action:/*selector*/ forControlEvents:/*UIControlEvents*/];
[btn removeTarget:/*target*/ action:/*selector*/ forControlEvents:/*UIControlEvents*/];

addTarget:action:forControlEvents: appledoc

removeTarget:action:forControlEvents: appledoc

You need to keep track which SEL was previously assigned so that you can remove it.

Happy coding :)

Upvotes: 1

Ujjwal Khatri
Ujjwal Khatri

Reputation: 836

You can get the button object from which the function is called in the following method declaration in "sender" object.

-(IBAction)buttonClicked:(id)sender

from this you can get the tag of the UIButton from which the function is called and perform the desired action.

Upvotes: 0

Sahil
Sahil

Reputation: 9226

why you want to set different actions? you can do this by set tag property of button. on action you can check tag value and can do different tasks i guess.

Upvotes: 0

Related Questions