开发者

IBAction on Button Press [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion.开发者_如何学编程 If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

A discussion. Which is more preferred:

A single IBAction with sender and a bunch of if statements ex: if (sender == self.whichbuttonpressed)

or

A single IBAction with sender and a switch statement ex: switch (send.tag) // short handed for easier reading

or

an IBAction for each button.

for the discussion lets say we have 6 or less buttons in the XIB file.

Thanks

PS make believe your grandma is reading your responses so no sarcasm please, be real.


Your implementation depends on your app, but consider this..

Say you were implementing a number pad, with 10 buttons (0 through 9). The only difference when pressing each button is the number is represents. So, if you set each button's tag to the numeric value the button is supposed to represent, handling 10 buttons with a single IBAction method becomes trivial:

    -(IBAction)buttonPress:(id)sender
    {
        UIButton *b = (UIButton *)sender;
        int value = b.tag;

        // Do something with value...
    }

I have used this approach for a variety of situations where a single view contains many buttons that all essentially do the same thing but can be distinguished behind the scenes by assigning a unique tag to each one.

If you have a view with many buttons but they are unrelated to one another, then each button should probably have it's own IBAction method. Doing so in this case keeps the unique implementation for each button's action much more readable and maintainable.


I prefer using tags, so

A single IBAction with sender and a switch statement ex: switch (send.tag) // short handed for easier reading

Last approach is reasonable if actions for different buttons are significantly different


Definitely an IBAction for each button. Clean. Simple. Enables you to add or remove an action or a button without editing and possibly introducing errors into the code for the other actions.


I actually the second and third ones you list. Tags are definitely easier to work with than a bunch of if statements. If I have a bunch of buttons on one view that are totally unrelated, I generally use separate IBActions. If they relate to each other then I use 1 IBAction so I don't have to repeat myself.

Example:

  1. Login and Signup buttons: different IBActions. One needs to push a VC, the other needs to make a webservice call for authentication.
  2. Breadcrumbs. In an app I'm working on right now we have a bar of breadcrumbs that are steps a user has to complete in order. I use 1 IBAction to handle those because 1. their states change in a consistent way and 2. they all essentially perform the same action (swapping in their corresponding view in the form).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜