通用应用程序中的 UIAlertController 和 UIPopoverController?

UIAlertController and UIPopoverController in a Universal App?

我正在为 iPhone 6S / 6S Plus / 和 iPad 外形设计一个通用应用程序。通常,在仅 iPhone 的应用程序上呈现 actionsheets / alertviews 是一种简单的方式。但是,当我尝试在 iPad 上显示这些内容时,我的应用程序崩溃了,并返回以下错误:

"Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'"

据我了解,当应用程序在 iPad 而不是常规操作 sheet 上 运行 时,我必须显示弹出窗口。为了上下文,操作 sheet 由自定义单元格中的按钮呈现,该单元格位于表视图中。

在通用应用程序中处理 UIAlertControllers / Action Sheets / UIPopoverControllers 的最佳方式是什么?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *titleToUse = @"";

//    switch (self.openGroup) {
//        case 0:
//            titleToUse = [self.deviceListData[indexPath.row] valueForKey:@"deviceName"];
//            break;
//            
//        case 1:
//            titleToUse = [self.computersData[indexPath.row] valueForKey:@"deviceName"];
//            break;
//            
//        case 2:
//            titleToUse = [self.mobileData[indexPath.row] valueForKey:@"deviceName"];
//            break;
//            
//        case 3:
//            titleToUse = [self.smartData[indexPath.row] valueForKey:@"deviceName"];
//            break;
//            
//        default:
//            break;
//    }

    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:titleToUse message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        // Cancel button tappped.

    }]];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Get More Info" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        AlertDetailModal *alertDetail = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"alertDetailModal"];

        alertDetail.delegate = self;
        alertDetail.securityLevel = self.securityLevel;

        UINavigationController *modalNavCon = [[UINavigationController alloc] initWithRootViewController:alertDetail];
        [self presentViewController:modalNavCon animated:YES completion:nil];

    }]];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Bandwidth Profile" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    }]];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Alert Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    }]];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Security Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    }]];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Unblock Connection" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    }]];

    // Present action sheet.
    [self presentViewController:actionSheet animated:YES completion:nil];

}

它的普遍性这一事实无关紧要。无论如何,您都将警报控制器的 popoverPresentationController 设置为相同。然后它将在所有设备上正确显示。

在这种情况下,您应该将 sourceView 设置为 tableView 并将 sourceRect 设置为所选行的矩形。

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:titleToUse message:nil preferredStyle:UIAlertControllerStyleActionSheet];
actionSheet.popoverPresentationController.sourceView = tableView;
actionSheet.popoverPresentationController.sourceRect = [tableView rectForRowAtIndexPath:indexPath];