Pass different parameters to an IBAction

前端 未结 7 1455
面向向阳花
面向向阳花 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条回答
  •  青春惊慌失措
    2020-12-08 23:38

    Give your various UIButton instances different tag property values.

    In your IBAction method -myMethod:, you might then do something like:

    - (void) myMethod:(id)sender {
        switch (sender.tag) {
            case (firstButtonTag):
               doFooStuff;
               break;
            case (secondButtonTag):
               doBarStuff;
               break;
            // etc.
        }
    }
    

    The values firstButtonTag and secondButtonTag can be stored in an enum if you want to make this easy to maintain.

提交回复
热议问题