我使用以下代码进行警报视图,它可以在iPhone中使用。但是当我在iPad上运行它时会出现以下错误:
由于未捕获的异常'NSGenericException'而终止应用程序,原因是:'您的应用程序已呈现样式UIAlertControllerStyleActionSheet的UIAlertController()。具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover。您必须通过警报控制器的popoverPresentationController为此弹出窗口提供位置信息。您必须提供sourceView和sourceRect或barButtonItem。如果在显示警报控制器时不知道此信息,您可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供它。'
警报视图代码如下:
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上工作......
答案 0 :(得分:0)
在iPad上,警报将使用UIPopoverPresentationController显示为弹出窗口,它要求您为弹出窗口的显示指定一个锚点。
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];