首页 文章

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

提问于
浏览
6

我正在开发适用于iPhone 6S / 6S Plus /和iPad外形的通用应用程序 . 通常,在仅限iPhone的应用程序上呈现动作表/警报视图是一种简单的方式 . 但是当我尝试在iPad上显示这些应用程序时,我的应用程序崩溃,返回以下错误:

“由于未捕获的异常终止应用程序'NSGenericException',原因:'您的应用程序已呈现样式UIAlertControllerStyleActionSheet的UIAlertController() . 具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover . 您必须通过警报控制器提供此弹出窗口的位置信息popoverPresentationController . 您必须提供sourceView和sourceRect或barButtonItem . 如果在显示警报控制器时不知道此信息,您可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供它 .

我的理解是,当应用程序在iPad上运行而不是传统的操作表时,我必须显示一个弹出窗口 . 为了上下文,操作表由自定义单元格中的按钮显示,该按钮位于tableview中 .

在通用应用程序中处理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];

}

1 回答

  • 17

    它的普遍性无关紧要 . 无论如何,您都可以设置警报控制器的 popoverPresentationController . 然后它将在所有设备上正确显示 .

    在这种情况下,您应该将 sourceView 设置为 tableViewsourceRect 到所选行的矩形 .

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

相关问题