3

I'm still quite the beginner at iOS and so I've been doing lots of tutorials, lately.

Let's say I was making an app such as a calculator, with let's say 24 buttons. I've seen example code where the button's label gets used to figure out what button it is, but that seems really kludgey, especially when trying to translate the app.

So is it better to just bite the bullet and have one IBOutlet for each and every button instead? why or why not?

If not, what would be the most elegant way to go about doing this, while staying in the MVC paradigm?


Ok I just was looking back at my code and now i feel more like a noob than before... I really was talking about IBActions, not so much IBOutlets... Should I have a whole bunch of IBActions for the different buttons? here's what it looks like right now in the viewController.h file:

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;
- (IBAction)dotPressed:(UIButton *)sender;
- (IBAction)button_mClear_Pressed:(UIButton *) sender;
- (IBAction)button_mPlus_Pressed:(UIButton *) sender;
- (IBAction)button_mMinus_Pressed:(UIButton *) sender;
- (IBAction)button_mRecall_Pressed:(UIButton *) sender;
- (IBAction)button_AC_Pressed:(UIButton *) sender;
- (IBAction)button_PlusMinus_Pressed:(UIButton *) sender;

why does that just feel repetitive and inelegant to me?

Dave Kliman
  • 441
  • 4
  • 17

3 Answers3

3

Typically, you'd have similar buttons all trigger the same action, the idea being that similar button actions should have some common code between them. You then use the tag property to identify which button was clicked. E.g., number buttons trigger a specific action, operator buttons trigger another action, and so on.

- (void)didClickOperatorButton:(id)button 
{
    switch ([button tag])
    {
        case kAdditionOperation: 
            // Do the addition operation ...
            // etc..

You can set the tag property on any control in Interface Builder.

Darren
  • 25,520
  • 5
  • 61
  • 71
  • That sounds reasonable. That's to some extent what I was doing. I'll look into the tag thing... that makes good sense. – Dave Kliman Jun 21 '11 at 01:59
2

If you use IBOutlets and wire them up in Interface Builder/Xcode 4 is more a matter of taste, than a programming decision. And doing so or not does not necessarily affect the mvc paradigm.

It is your choice, if you keep 24 IBOutlets in your viewcontroller and load the buttons from a nib, as it is maybe easier to arrage them in your interface, or to have an array full of buttons, and add them to your view programmatically and set them up with the right actions.

You can also have the buttons in different nibs for different viewcontroller — lets say for the number pad, the simple commands and the higher commands and functions. each of the viewcontrollers would have a delegate of a certain protocol, which all would be implemented by on 'BrainController'.This setup might be a bit overkill for a simple calculator, but would allow you to use nibs, without a viewcontoller overcrowded with IBOutlets. And you could re-use oarts of it in other project, i.e. the numberpad in an app with a remote control interface.

Zoe
  • 27,060
  • 21
  • 118
  • 148
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • ah i like the idea of a BrainController... so would all the rest of the view controllers be subclasses of that one? so for example on apple's calculator, there are two views... the portrait view with its 22 buttons, as well as the landscape view with its 46 buttons, wider display, higher precision, deg/rad display... how exactly would i set that up in terms of nibs and view controllers, especially reusing the original buttons that are on the less advanced view? – Dave Kliman Jun 28 '11 at 13:48
1

If you use XIB's and a lot of objects, then yes. If you plan on making the object do something special like disable the button during some method call later in code, then YES, hook up an IBOutlet. If you are only connecting the buttons to IBActions, then NO, just connect any button (without IBOutlet) to your IBAction, this will save you on connecting a bunch of objects.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186