简体   繁体   中英

UIAlerController Action Sheet does not respond the second time it's called

I have something like the following code. The Action sheet runs doSomething OK when it appears for the first time (in a button IBAction), but when it appears the second time, nothing happens the Action sheet just disappear without calling do something. Any idea?

@implementation ...

- (void) setActions {
    UIAlertAction *opt1 = [UIAlertAction actionWithTitle:@"Option 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self doSomething:@"opt1"];}];

    UIAlertAction *opt2 = [UIAlertAction actionWithTitle:@"Option 2"    style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self doSomething:@"opt2"];}];

    UIAlertAction *opt3 = ...

    self.opt1 = opt1;
    self.opt2 = opt2;
    self.opt3 = opt3;

- (void) showActionSheet {

    ...
UIAlertController *selectAS = [UIAlertController alertControllerWithTitle:@"Select Options"
 message:@"msg" preferredStyle:UIAlertControllerStyleActionSheet];

    if (xyz) {                  
          [selectAS addAction:self.opt1];
          [selectAS addAction:self.opt2];
        }
    else{            
          [selectAS addAction:self.opt1];
          [selectAS addAction:self.opt3];                    
        }
   [self presentViewController:selectqAS 
    animated:YES completion:nil];
        }

- (void) doSomething: (NSString *) opt{



  ....
    }

Glad we got you up and running. My guess is your methods are getting lost in translation. You have methods intertwining each other which can be causing the confusion, specifically with self.opt1 . per my comment, now that iOS8 has introduced UIAlertController , it comes with completion handlers, you should plan accordingly to that: something like the following :

-(IBAction)showActionSheet {
    UIAlertController *selectAS = [UIAlertController alertControllerWithTitle:@"Select Options" message:@"msg" preferredStyle:UIAlertControllerStyleActionSheet];

     UIAlertAction *opt1 = [UIAlertAction actionWithTitle:@"Option 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //Don't have to call another method, just put your action 1 code here. This is the power of completion handlers creating a more structured outline
     }];

     UIAlertAction *opt2 = [UIAlertAction actionWithTitle:@"Option 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //Don't have to call another method, just put your action 2 code here. This is the power of completion handlers creating a more structured outline
     }];

     UIAlertAction *opt3 = ...

     if (xyz) {
       [selectAs addAction:opt1];
       [selectAs addAction:opt2];
     } else {
       [selectAs addAction:opt1];
       [selectAs addAction:opt3];
     }        

     [self presentViewController:selectAs animated:YES completion:nil];

 }

Much more cleaner and actually uses the UIAlertController for it's intended purposes, no other method calls needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM