Pass different parameters to an IBAction

前端 未结 7 1458
面向向阳花
面向向阳花 2020-12-08 22:39

My iPhone app has many buttons and I want all the buttons to call the same method, but with different parameters.

For example I want tapping one button to call the m

7条回答
  •  旧时难觅i
    2020-12-08 23:23

    You don't. The only parameter is the sender object, which you may use to have a different behavior, but what I'd do is define 2 action methods, which simply in turn call the same method with a different parameter, i.e. you'd have:

    - (IBAction)button1:(id)sender
    {
        [self doStuff:kButton1];
    }
    
    - (IBAction)button2:(id)sender
    {
        [self doStuff:kButton2];
    }
    
    - (void)doStuff:(ParamType)param;
    {
        ...
    }
    

    In defense of that method (no pun intended), I'd add that it makes clearer when you review your UI with Interface Builder to see that different buttons actually have different effects, which is harder to tell if they all call whateverAction:

提交回复
热议问题