我如何继续添加 UIButton 并以编程方式使用不同的操作设置它们



>假设我有一个存储项目列表的容器。通过添加这些项目,我必须为每个项目添加一个 UIView。我想创建一个删除按钮,允许用户删除他们不需要的项目。如何继续添加这些按钮并使用不同的操作将它们分开?就像这个按钮用于删除项目 A,而那个按钮用于删除项目 B?附言这种情况不允许使用tableView,我已经处理了视图堆叠部分。如果您需要我显示任何代码,请随时询问。

更新:

添加项的代码:

-(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)];
您需要在

为附件创建UIView后为按钮提供标签。

保持方法名称相同,并尝试使用标记值。

例如:

button.tag = 1000;//创建它时。

在方法中,您将 UIButton 作为参数传递

方法主体内部

NSInteger tag = button.tag

[array removeObjectAtIndex:tag];

希望我理解你的情况,

您希望动态设置selector。假设您有以下selector声明。

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

现在需要用NSArray或其他存储来存储它们。让我们将它们保存在数组中。为此,您需要将它们转换为NSString,如下所示

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

现在,您可以从NSStringSELSEL来回NSString,如下所示。

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

现在,您可以使用

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

addTarget:action:forControlEvents: appledoc

removeTarget:action:forControlEvents: appledoc

您需要跟踪之前分配了哪个SEL,以便可以将其删除。

快乐的编码:)

为什么要

设置不同的操作? 您可以通过设置按钮的标签属性来执行此操作。 在操作中,您可以检查标签值,并可以执行我猜的不同任务。

您可以在"sender"对象的以下方法声明中获取从中调用函数的按钮对象。

-(IBAction)buttonClicked:(id)sender

由此,您可以获取从中调用函数的 UIButton 的标签并执行所需的操作。

最新更新