Pass different parameters to an IBAction

前端 未结 7 1454
面向向阳花
面向向阳花 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:17

    The so called "IBActions" must have one of these signatures:

    -(void)action;
    -(void)actionWithSender:(id)sender;
    -(void)actionWithSender:(id)sender event:(UIEvent*)event;
    

    You cannot add any other parameters. Nevertheless you can use sender (which is button1 or button2 in your case) to get the parameter:

    -(void)actionWithSender:(UIButton*)sender {
       NSString* parameter;
       if (sender.tag == 1)   // button1
         parameter = @"foo";
       else                   // button2
         parameter = @"bar";
       ...
    }
    

提交回复
热议问题