gpt4 book ai didi

objective-c - 如何使用 UIAlertController 修复运行时错误

转载 作者:太空狗 更新时间:2023-10-30 03:42:11 27 4
gpt4 key购买 nike

我将这段代码放在 UIVIewController(XCode 6.1、iOS 8.1.1)中:

[UIAlertController showActionSheetInViewController:self
withTitle:@"Test Action Sheet"
message:NSLocalizedString(@"Are you sure you want to delete ALL appointments?",nil)
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Yes"
otherButtonTitles:@[@"No"] // same as Cancel
tapBlock:^(UIAlertController *controller, UIAlertAction *action, NSInteger buttonIndex){
if (buttonIndex == UIAlertControllerBlocksCancelButtonIndex) {
NSLog(@"Cancel Tapped");
} else if (buttonIndex == UIAlertControllerBlocksDestructiveButtonIndex) {
NSLog(@"Delete Tapped");
} else if (buttonIndex >= UIAlertControllerBlocksFirstOtherButtonIndex) {
NSLog(@"Other Action Index %ld", (long)buttonIndex - UIAlertControllerBlocksFirstOtherButtonIndex);
}
}];

当我运行它时,出现了这个运行时错误:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7fdfe3324f00>) 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.'

我需要做什么才能完成这项工作? (我查看了 SO 和 Google,没有发现任何具体内容)。我很感激我能得到的任何帮助...

更新我在没有第 3 方代码的情况下重写了它;添加了这段代码,现在可以使用了!

    UIAlertController * view=   [UIAlertController
alertControllerWithTitle:@"My Title"
message:@"Select your Choice"
preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];

}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];

}];


[view addAction:ok];
[view addAction:cancel];

view.popoverPresentationController.sourceView = self.view;
view.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 1.0, 1.0);
[self presentViewController: view animated:YES completion:nil];

最佳答案

出现您收到的错误消息是因为您在 iPad 上运行 iPhone 代码。要在 iPad 上使用,您必须设置 alertController 的 popoverPresentationController。也可以在不进行草率的尺寸计算的情况下生成源矩形。下面是一个完整的方法,展示了按下按钮时如何遇到代码。按照您想要的方式设置 AlertController 后,您将获得它的 popoverPresentationController 并将其设置为与 iPad 一起使用。在下面的方法中,按下的按钮是发送者。所以我们将发送者投回那个按钮,然后使用按钮来设置矩形。没有杂乱的尺寸需要计算。现在,如果您在 iPad 上运行代码,弹出窗口将不会显示“取消”按钮(在 iPhone 上会出现)。这是设计使然。如果您查看 Apple UIPopoverController 文档,您会看到弹出窗口通过点击它的外部被取消。

- (IBAction)showImagePickerButtonTapped:(id)sender;
{
BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
BOOL isPhotoLibraryAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];

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

[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];

if (isCameraAvailable) {
[alertController addAction:[UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera];
}]];
}

if (isPhotoLibraryAvailable) {
[alertController addAction:[UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}]];
}

// The following lines are needed for use with the iPad.
UIPopoverPresentationController *alertPopoverPresentationController = alertController.popoverPresentationController;
UIButton *imagePickerButton = (UIButton*)sender;
alertPopoverPresentationController.sourceRect = imagePickerButton.frame;
alertPopoverPresentationController.sourceView = self.view;

[self showDetailViewController:alertController sender:sender];
}

关于objective-c - 如何使用 UIAlertController 修复运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27681887/

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