报错信息:
Your application has presented a UIAlertController (<UIAlertController: 0x100b790f0>) 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.
复制代码ActionSheet样式弹框:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"act1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"act2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[alert addAction:action2];
[alert addAction:cancle];
[self presentViewController:alert animated:YES completion:nil];
复制代码设置仅在iPhone: 路径: TARGETS -> Development Info -> Devices
上面创建UIAlertControllerStyleActionSheet样式弹框的代码在iPhone手机上运行时没有问题,运行效果如图(四周存在黑边是因为Xcode设置仅在iPhone运行 而模拟器放在了iPad上不适配所致): 此时可看到UIAlertControllerStyleActionSheet运行效果iPhone与iPad效果一致.
设置Universal:
设置机型为Universal(即: 手机和iPad均支持) 在iPhone上运行效果与上一致,而在iPad上运行时 导致崩溃
解决:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"act1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"act2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[alert addAction:action2];
[alert addAction:cancle];
UIPopoverPresentationController *popover = alert.popoverPresentationController;
if (popover)
{
popover.sourceView = self.view;
popover.sourceRect = CGRectMake(0, 0, 100, 100);
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
}
[self presentViewController:alert animated:YES completion:nil];
复制代码
本文介绍了在iOS开发中使用UIAlertController呈现ActionSheet样式弹框时遇到的问题:当应用支持iPad时,若未正确配置popoverPresentationController,则会导致崩溃。文章提供了具体的代码示例及解决方案。
3992

被折叠的 条评论
为什么被折叠?



