UIAlertController Action Sheet在第二次调用时没有响应

时间:2015-02-17 05:49:08

标签: ios uialertcontroller

我有类似下面的代码。当第一次出现(在按钮IBAction中)时,Action表运行doSomething OK,但是当它第二次出现时,没有任何反应,Action表只是消失而没有调用做某事。有什么想法吗?

@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{



  ....
    }

1 个答案:

答案 0 :(得分:1)

很高兴我们让你起来跑步。我的猜测是你的方法在翻译中迷失了。你有相互交织在一起的方法可能导致混淆,特别是self.opt1。根据我的评论,现在iOS8引入了UIAlertController,它带有完成处理程序,你应该相应地计划:如下所示:

-(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];

 }

更清洁,实际上使用UIAlertController是出于预期目的,不需要其他方法调用。

相关问题