PresentViewController无法处理动作表按钮

时间:2016-03-16 15:05:26

标签: ios objective-c ipad uiactionsheet

在我的代码中,我想使用actionsheet打开一个视图控制器作为presentviewcontroller。但当我点击动作表视图的按钮时,它的代码正在执行。

以下是调用操作表的代码:

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"Print Full Report",@"Print Only Selected Questions", nil];

//Creating Attributed String with System Font size of 30.0f
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Print Options" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:20.0f] , NSForegroundColorAttributeName : [UIColor grayColor]}];

//Accessing the Alert View Controller property from Action Sheet
UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];

//Setting Attributed Title property of Alert View Controller
[alertController setValue:attrString forKey:@"_attributedTitle"];

[actionSheet showInView:self.view];
actionSheet.tag = 100;

以下是委托和相关方法:

- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {
    // tag 1 = save options.  2 = clear options
    switch (buttonIndex) {

        case 0:
            [popup dismissWithClickedButtonIndex:0 animated:YES];
            pdfFlag=1;
            [self printReportAction];
            break;
        case 1:
            [popup dismissWithClickedButtonIndex:1 animated:YES];
            pdfFlag=2;
            [self printReportAction];
            break;

    }
}

-(void)printReportAction{
    [self presentViewController:previewer animated:YES completion:nil];
}

2 个答案:

答案 0 :(得分:0)

你是否将控制器初始化为现在?

如果是这样的话,检查VC的当前不是零 删除这些电话:

[popup dismissWithClickedButtonIndex:0 animated:YES];

答案 1 :(得分:-1)

可能有几个原因导致代码无效。但是,在iOS 8.3中不推荐使用ActionSheets。我建议改用UIAlertController。使用UIAlertController可以使用块,这使得跟踪问题变得更加容易和清晰。

我已经模拟了一个示例项目,它重新创建了您要完成的任务。 https://github.com/andyast/ActionSheetTest