开发者

Comparing uibutton clicks

I want to be able to control certain functions when a button is clicked either by having a bool or another variable know how many times the button was clicked. For example if the button was clicked once i want to make it display an NSLOG of lets say 1, if its pressed twice i want NSLOG of 2,开发者_高级运维 however once you press it again I cant find a way to get it back to 1..


You could link it to a IBAction containing the following code:

- (IBAction) ButtonAction:(id)sender {
  static int x = 0;
  x++;
  if (x == 3)
    x = 1;
  NSLog(@"%d", x);
}

And link the button "click" action (TouchUpInside) to this action in interface builder.

Hope it helps!


I don't understand exactly what you're looking for, but maybe something like this?

enum ButtonSequentialAction
{
    kButtonSequentialAction1 = 0,
    kButtonSequentialAction2,
    kButtonSequentialAction3,
    kButtonSequentialActionTotal
};
...
- (void) buttonPress
{
    switch (m_CurrentButtonAction) // m_CurrentButtonAction is a member variable of the class
    {
        case kButtonSequentialAction1:
        {
            // do action 1;
            break;
        }
        case kButtonSequentialAction2:
        {
            // do action 2;
            break;
        }
        case kButtonSequentialAction3:
        {
            // do action 3;
            break;
        }
        default:
        {
            // crap, shouldn't get here.
            break;
        }
    }
    m_CurrentButtonAction = (m_CurrentButtonAction + 1) % kButtonSequentialActionTotal;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜