0

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)];
Johnny Cheuk
  • 237
  • 2
  • 15

4 Answers4

2

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];

Abhishek
  • 358
  • 3
  • 10
1

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 :)

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • I'm kinda understand how your way works, but one thing I don't understand that much is how is the array works ? inside how do I append a selector which is a string with number? – Johnny Cheuk Mar 21 '16 at 08:48
  • take `NSMutableArray` instead of `NSArray`. Then add `NSString` like `[mutArr addObject: NSStringFromSelector(@selector(onPressB1:))]` – Ratul Sharker Mar 21 '16 at 08:51
  • I mean in this: NSStringFromSelector(@selector(onPressB1:))]; This is much like a hard code in the selector. Can I actually put something like: int i = 0; NSStringFromSelector(@selector([NSString stringWithFormat:@"button%d",i])); like this? – Johnny Cheuk Mar 21 '16 at 08:55
  • now i understand, you want to choose the selector dynamically, but you need use a specific pattern to list all the usable `selector`. [this link](http://stackoverflow.com/questions/330030/list-selectors-for-objective-c-object) shows how to list all the available `selector` of a `NSObject` – Ratul Sharker Mar 21 '16 at 09:06
  • That was pretty complicated to understand :( But I will give it a try :) – Johnny Cheuk Mar 21 '16 at 09:17
0

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.

Sahil
  • 9,096
  • 3
  • 25
  • 29
0

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.

Ujjwal Khatri
  • 716
  • 1
  • 6
  • 19