gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:36:56 26 4
gpt4 key购买 nike

我正在为 iPhone 6S/6S Plus/和 iPad 外形设计一个通用应用程序。通常,在仅限 iPhone 的应用程序上显示操作表/警报 View 是一种简单的方式。但是,当我尝试在 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 上运行时,我必须显示一个弹出窗口,而不是传统的操作表。出于上下文考虑,操作表由表格 View 中自定义单元格中的按钮呈现。

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

}

最佳答案

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

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

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

关于ios - 通用应用程序中的 UIAlertController 和 UIPopoverController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36947250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com