V
V
Votetoda2014-12-09 22:36:10
Objective-C
Votetoda, 2014-12-09 22:36:10

How to detect a button click?

Hello! the question may seem stupid, ridiculous, but I appeal to the court because I did not find an answer to it. I met several languages ​​\u200b\u200bin which this method works fine, but this was not found here. It is necessary to determine which button was pressed, that is, if (button. click){bla bla bla} with the help of such a small code it was possible to define this action, can you tell me with what help such an action was implemented in objective c?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
ManWithBear, 2014-12-09
@Votetoda

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self  action:@selector(myLovelyFunction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Touch me, baby!" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

In turn, the called function looks something like this:
- (void)myLovelyFunction:(id)sender {
    // do something
}

UPD. If many many buttons call the same function, but in different ways, then you can do it like this:
when creating a button:
And in the function
- (void)myLovelyFunction:(id)sender {
   UIButton *button = (UIButton*)sender;
   if (button.tag == 91) {
      // super
   } else {
      // good too
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question