Ios 未捕获异常的NSGenericeException:应用程序提供了UIAlertControllerStyleActionSheet样式的UIAlertController
我正在开发一款在iPhone上运行的应用程序,但当我试图在iPad上运行时,它崩溃了 这是我的密码:Ios 未捕获异常的NSGenericeException:应用程序提供了UIAlertControllerStyleActionSheet样式的UIAlertController,ios,objective-c,exception,uialertcontroller,uimodalpresentationstyle,Ios,Objective C,Exception,Uialertcontroller,Uimodalpresentationstyle,我正在开发一款在iPhone上运行的应用程序,但当我试图在iPad上运行时,它崩溃了 这是我的密码: - (void)parseCountryStates:(NSDictionary *)json { countryPickerView.hidden = TRUE; NSDictionary *listing = [json objectForKey:@"country"]; countryArray = [listing allValues]; coun
- (void)parseCountryStates:(NSDictionary *)json
{
countryPickerView.hidden = TRUE;
NSDictionary *listing = [json objectForKey:@"country"];
countryArray = [listing allValues];
countryIDArray = [listing allKeys];
[countryPickerView reloadAllComponents];
alertController = [UIAlertController
alertControllerWithTitle:@"Select Service Type"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
int count = (int)[countryPickerView numberOfRowsInComponent:0];
for (int i = 0; i < count; i++)
{
UIAlertAction* button = [UIAlertAction
actionWithTitle:[[countryPickerView delegate] pickerView:countryPickerView titleForRow:i forComponent:0]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
countryField.text = [action title];
countryStr = countryField.text;
if ([countryArray containsObject:countryStr]) {
countryidStr = [countryIDArray objectAtIndex:[countryArray indexOfObject:countryStr]];
NSLog(@"CountryidStr %@",countryidStr);
[self getState];
}
}];
[alertController addAction:button];
}
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
// UIAlertController will automatically dismiss the view
}];
[alertController addAction:cancel];
[self presentViewController:alertController animated:true completion:nil];
}
我正在分享它的崩溃日志
***由于未捕获的异常“NSGenericeException”而终止应用程序,原因:“您的应用程序已呈现的UIAlertController为”
样式UIAlertControllerStyleActionSheet。英语的现代表达风格
具有此样式的UIAlertController是UIModalPresentationPopover。你
必须通过警报提供此popover的位置信息
控制器的popoverPresentationController。您必须提供
sourceView和sourceRect或barButtonItem。如果此信息是
不知道何时显示警报控制器,您可以在
UIPopoverPresentationControllerDelegate方法
-准备正式报告
实际上,ipad上不允许使用AlertController,相反,您可以使用弹出窗口来显示某种警报 程序上 使用故事板 解散流行音乐 有一个新的协议称为UIPopoverPresentationControllerDelegate,它是在由于旋转或接口更改而导致解雇和位置更改时调用的。如果我们愿意,我们甚至可以防止爆米花被解雇。以下是我们可以实施的三种方法:
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
// called when a Popover is dismissed
}
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
// return YES if the Popover should be dismissed
// return NO if the Popover should not be dismissed
return YES;
}
- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing _Nonnull *)view {
// called when the Popover changes position
}
不要忘记遵守协议,并将委托设置为您的反应类。将源代码视图和源代码rect添加到您的alertController
[[alertController popoverPresentationController] setSourceView:self.view];
[[alertController popoverPresentationController] setSourceRect:CGRectMake(0,0,1,1)];
[[alertController popoverPresentationController] setPermittedArrowDirections:UIPopoverArrowDirectionUp];
[self presentViewController:alertController animated:true completion:nil];
iPad上不允许使用UIPopover,但正如其他答案所示,有一种方法可以做到这一点。这是一个Swift 5.x版本
let ac = UIAlertController(title: "Some title goes here", message: nil, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: "Some button name", style: .default) {
[unowned self] _ in
// stuff to do goes here
self.doSomething()
})
// iPad specific code
ac.popoverPresentationController?.sourceView = self.view
let xOrigin = nil // Replace this with one of the lines at the end
let popoverRect = CGRect(x: xOrigin, y: 0, width: 1, height: 1)
ac.popoverPresentationController?.sourceRect = popoverRect
ac.popoverPresentationController?.permittedArrowDirections = .up
present(ac, animated: true)
将let xOrigin=nil行替换为下面的一行将控制popover显示在导航栏下方的位置。如果在iPad上有导航栏下方的控件,还可以在不同元素的边界或帧中将x和y更改为适当的值
左上角
let xOrigin = 0
中上部
let xOrigin = self.view.bounds.width / 2
右上角
let xOrigin = self.view.bounds.width
希望这能有所帮助。ipad不支持操作表您必须将其显示为popover控制器太多类似问题找到它可能重复,而不是将其标记为重复请回答它而不是询问请搜索它
let xOrigin = 0
let xOrigin = self.view.bounds.width / 2
let xOrigin = self.view.bounds.width