gpt4 book ai didi

ios - UIAlertController 中的 UIPickerView 仅在 iPad 中给出错误,而在 iPhone 中正确?

转载 作者:行者123 更新时间:2023-11-30 13:57:39 24 4
gpt4 key购买 nike

我使用以下代码进行警报 View ,它在 iPhone 中工作。但是当我在 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.'

警报 View 的代码如下:

let alertView = UIAlertController(title: "Select Launguage", message: "\n\n\n\n\n\n\n\n\n\n", preferredStyle: UIAlertControllerStyle.ActionSheet);

pickerView.center.x = self.view.center.x

alertView.view.addSubview(pickerView)

let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)

alertView.addAction(action)
presentViewController(alertView, animated: true, completion: nil)

请帮助我。为什么它不能在 iPad 上运行...

最佳答案

在 iPad 上,警报将使用 UIPopoverPresentationController 显示为弹出窗口,它要求您指定用于呈现弹出窗口的 anchor 。

UIViewController *self; // code assumes you're in a view controller
UIButton *button; // the button you want to show the popup sheet from

UIAlertController *alertController;
UIAlertAction *destroyAction;
UIAlertAction *otherAction;

alertController = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
destroyAction = [UIAlertAction actionWithTitle:@"Remove All Data"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
// do destructive stuff here
}];
otherAction = [UIAlertAction actionWithTitle:@"Blah"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// do something here
}];
// note: you can control the order buttons are shown, unlike UIActionSheet
[alertController addAction:destroyAction];
[alertController addAction:otherAction];
[alertController setModalPresentationStyle:UIModalPresentationPopover];

UIPopoverPresentationController *popPresenter = [alertController
popoverPresentationController];
popPresenter.sourceView = button;
popPresenter.sourceRect = button.bounds;
[self presentViewController:alertController animated:YES completion:nil];

关于ios - UIAlertController 中的 UIPickerView 仅在 iPad 中给出错误,而在 iPhone 中正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33414801/

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