就是这样:我正在使用一些遗留代码。所以我现在不能真的(可行地)改变架构。
我有一个创建字符串数组的文件,位于VC1
:
NSMutableArray *arrButtons = [NSMutableArray array];
[arrButtons addObject:data];
[arrButtons addObject:share];
[VC2 showButtons:arrButtons];
然后在我的VC2
代码上,我有:
-(void)showButtons:(NSMutableArray *)arrButtons {
for (int i=0; i<arrButtons.count; i++) {
UIButton *btn = [_popupView viewWithTag:i+5000];
[btn setTitle:[arrButtons objectAtIndex:i] forState:UIControlStateNormal];
//this is the code I am trying out, I just need to addtarget to data, not the rest of the array.
if ([arrButtons containsObject:data]) {
//this is adding to all buttons, not just data. Figure out a way to add this action to only data.
btn.[index: data]
[btn addTarget:self action:@selector(arrayButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
}
VC1
当我们第一次将目标添加到数组中时,只添加目标是有意义的。但我不能,因为当它被创建时,它只是一个字符串。我想指出该按钮确实显示在屏幕上。但我不知道如何访问数组中的特定按钮以向其添加目标。
我能想出的最佳解决方案是我需要,addTarget
但如果其他人对如何解决此类问题有任何指示或想法,我将不胜感激。
ps 我知道如何从 IB 连接 IBActions,问题是这是一个 100% 以编程方式创建的按钮,当创建时,它实际上只是一个字符串,而不是按钮。所以addTarget
不可用。