Pass different parameters to an IBAction

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

    You can't pass parameters through an IBAction. What I usually do is give the buttons the unique tag in IB. THe tag is an integer value so I u then use a simple lookup table to convert the tag to some value.

    In this case, three buttons but tags 1 to 3:

    - (IBAction) buttonPressed: (UIButton*) sender
    {
        static const NSString* names = { @"Foo", @"Bar", @"Baz" };
        id tag = [sender tag];
        if (tag >= 1 && tag <= 3) {
            NSLog(@"Button pressed is %@", names[tag]);
        }
    }
    

提交回复
热议问题