How can I use two actions for UIButton
click?I have a UIAlertView showing with two button.Play again and exit.Now i want to execute two method in the click event of these buttons.
Asked
Active
Viewed 6.0k times
40

vikingosegundo
- 52,040
- 14
- 137
- 178

sohel14_cse_ju
- 2,481
- 9
- 34
- 55
-
Please create a bit more context, your question contains the word UIAlertView and the description doesn't hint how that fits in. – Nick Weaver Apr 23 '11 at 10:10
-
i have updated the question.please check it now.and tell me how can i do this? – sohel14_cse_ju Apr 23 '11 at 10:14
-
i couldn't solve the problem yes...can you help me? – sohel14_cse_ju Apr 23 '11 at 11:52
1 Answers
123
UPDATE - May 2016
UIAlertView is deprecated. You can now use UIAlertController as explained here.
Old Answer with UIAlertView
You can create a UIAlertView like this
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"Do you really want to reset this game?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"reset", nil]; [alert show];
To handle AlertView button click, you have to conform to
UIAlertViewDelegate
protocol.@interface YourViewController:UIViewController<UIAlertViewDelegate>{ ....... ....... }
Then implement
UIAlertViewDelegate
protocol methods,- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == [alertView cancelButtonIndex]){ //cancel clicked ...do your action }else{ //reset clicked } }

Community
- 1
- 1

Krishnabhadra
- 34,169
- 30
- 118
- 167
-
1where should i put this funciton : - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { //cancel clicked ...do your action } else if (buttonIndex == 1) { //reset clicked } } – sohel14_cse_ju Apr 23 '11 at 10:20
-
1
-
oops yes..do one thing too, in the .h file of your controller confirm to UIAlertViewProtocol, ie if your controller name is SomeController in the .h file when we declare @interface SomeController:UIViewController
– Krishnabhadra Apr 23 '11 at 10:21 -
my .h file starting like this already : how can i use another delegate here?@interface GameTableView : UITableViewController – sohel14_cse_ju Apr 23 '11 at 10:39
-
thank you so much...finally i made your suggestion working....credit goes to you.....have a coffee by my name...:) – sohel14_cse_ju Apr 23 '11 at 12:11
-
Is there a constant defined for cancel button? It would be great instead of 0 check. – Gökhan Barış Aker Dec 09 '12 at 16:23
-
11
-
@RahulHawge No longer dabbles in iOS, so bit slow. Thanks for the pointer. Just updated the answer to mention about deprecation. – Krishnabhadra May 09 '16 at 06:41