multiple uibutton execute same function in objective c
I'm building an application that contain many buttons (generated dynamically). All the buttons should execute the same function when 开发者_运维问答pressed. My question is how to know which button has been pressed when the function is called, in order to perform the correct job.
The button instance that sends an action message to your action handler will be the sender
parameter passed to the handler. Just compare this against the button instances you have stored.
Alternatively, and slightly kludgy, use the tag
field of UIView (the superclass of UIButton
) to differentiate your buttons, and check the tag of sender
.
Try identifying the sender by a tag on the function that is being executed. That way you will know which button was pressed. You can assign the tag the moment you generate the buttons dynamically.
Optionally you could use a NSDictionary to save all the button tags that are being generated or an Array and then use it accordingly.
精彩评论