0

Here it is: I am working with some legacy code. So I can't really (feasibly) change the architecture at this point.

I have one file that creates an array of strings, in VC1:

   NSMutableArray *arrButtons = [NSMutableArray array];
[arrButtons addObject:data];
[arrButtons addObject:share];
[VC2 showButtons:arrButtons];

Then on my VC2 code , I have:

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

It would make sense to just add the target in VC1 when we first add it to the array. BUT I can't because when it is created, it is just a string. I'd like to point out that the button DOES show up on screen. But I don't know how to access that specific button in the array to add a target to it.

The best solution I can come up with is that I need to addTarget but if anyone else has any pointers or ideas on how something like this can be resolved, I would really appreciate it.

p.s. I know how to connect IBActions from IB, the problem is this is a button created 100% programmatically, and when created, it is really just a string, not a button. So addTarget is not available.

valeriana
  • 161
  • 1
  • 16

1 Answers1

0

I think that the problem in VC2 is in:

if ([arrButtons containsObject:data]) {

If I'm understanding this correctly, arrButons will always contain the data string. Assuming that data is the variable representing title of the button you want to add a target to, I'd put this instead:

if ([arrButtons[i] isEqualToString: data]) {

Please let us know how it goes.

  • @valeriana how did it go? Were you able to fix the problem? – Roberto Garrido Feb 13 '19 at 06:50
  • hi , no, it didn’t yet. I still have to figure out how to call only data, because it still adds the action to all the buttons. So I’m still working on it. Will definitely update as soon as I have an answer . Thanks so much – valeriana Feb 13 '19 at 13:33
  • @valeriana what does data variable contain in your VC2 code? – Roberto Garrido Feb 14 '19 at 13:23
  • Hi @RobertoGarrido I am still working on this. data is a button. In VC1 an array of strings is created `arrButtons` which is then passed to VC2 and every string is made into a button. `data` is first a string, then a button. – valeriana Feb 14 '19 at 16:29
  • @valeriana if data is a button in VC2 you are trying to find a UIButton inside an array of NSString (arrButtons is an array of NSString). I thought that data in VC2 was a string. – Roberto Garrido Feb 14 '19 at 17:19
  • I think that in VC2 inside that `showbuttons` call `arrButtons` becomes an array of buttons. Like I mentioned this is legacy code, and I'm still a bit confused about it. But I can tell you that a button does show up fine on the view. Just don't know how to `addAction ` to it. Or make it perform any kind of work. By the way, what I need the button to do is to `presentViewController:` to transition to a new vc – valeriana Feb 14 '19 at 18:00