Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

ios - UIAlertView Button Action

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.

question from:https://stackoverflow.com/questions/5763581/uialertview-button-action

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

UPDATE - May 2016

UIAlertView is deprecated. You can now use UIAlertController as explained here.

Old Answer with UIAlertView

  1. 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];
    
  2. To handle AlertView button click, you have to conform to UIAlertViewDelegate protocol.

    @interface YourViewController:UIViewController<UIAlertViewDelegate>{
      .......
      .......
    }
    
  3. Then implement UIAlertViewDelegate protocol methods,

    - (void)alertView:(UIAlertView *)alertView 
                       clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex == [alertView cancelButtonIndex]){
          //cancel clicked ...do your action
        }else{
          //reset clicked
        }
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...