Pass different parameters to an IBAction

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

    the real reason You cannot add additional parameter is that UIKIT will push params on the stack. so the only way is to use tags. A DIRTY way can be to convert a pointer to int and tagging the button with it:

    myStruct params;
    // fill params:
    params.x=....
    params.y=....
    params.z=....
    
    
    UIButton * btn = [UIButton......]; // create or use one from XIB
    btn.tag = (int)¶ms;
    
    ... in Call back:
    -(IBActions) doIt:(id)sender
    {
      myStruct * paramsPtr = (myStruct*)tag;
      int i = paramsPtr->x;
    

    NOTE: params MUST be keep static .. or allocate using malloc (more and more ugly code...). DO NOT use a local var: it will be allocated on stack so will be removed after exiting from the setup method.

提交回复
热议问题