我正在开发一个可以在iPhone上运行的应用程序,但是运行正常,但是当我尝试在iPad上运行时会崩溃
这是我的代码:
- (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];
}
我正在共享它的崩溃日志
***由于未捕获的异常'NSGenericException'而终止应用程序,原因:'您的应用程序呈现了一个UIAlertController() 样式UIAlertControllerStyleActionSheet。的modalPresentationStyle 具有这种样式的UIAlertController是UIModalPresentationPopover。您 必须通过警报提供此弹出窗口的位置信息 控制器的popoverPresentationController。您必须提供 sourceView和sourceRect或barButtonItem。如果此信息是 当您提出警报控制器时不知道,您可以在 UIPopoverPresentationControllerDelegate方法 -prepareForPopoverPresentation。
答案 0 :(得分:2)
实际上不允许在iPad上使用Alertcontrollers,而是可以使用弹出窗口来显示某种警报
以编程方式
UIViewController *newViewCont = [[UIViewController alloc] init];
newViewCont.view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 180, 180)];
newViewCont.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:newViewCont animated:YES completion:nil];
UIPopoverPresentationController *pop = [newViewCont popoverPresentationController];
pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
[pop setSourceView:myButton];
[pop setSourceRect:myButton.bounds];
使用情节提要
// grab the view controller we want to show
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Pop"];
// present the controller
// on iPad, this will be a Popover
// on iPhone, this will be an action sheet
controller.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:controller animated:YES completion:nil];
// configure the Popover presentation controller
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popController.delegate = self;
// in case we don't have a bar button as reference
popController.sourceView = self.view;
popController.sourceRect = CGRectMake(30, 50, 10, 10);
关闭弹出窗口
[self dismissViewControllerAnimated:YES completion:nil];
有一个称为UIPopoverPresentationControllerDelegate的新协议,在由于旋转或界面变化而解雇和位置变化时会被调用。如果我们愿意,我们甚至可以阻止Popover被解雇。这是我们可以实现的三种方法:
- (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
}
别忘了遵守协议,并将委托设置为您的反应班。
答案 1 :(得分:1)
将源代码视图和源代码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];
答案 2 :(得分:0)
在iPad上不允许使用UIPopovers,但是您可以按照其他答案的指示进行操作。这是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
行替换为下面的一行之一将控制弹出窗口在导航栏下方的显示位置。如果您将控件控制在iPad导航栏下方,则还可以在其他元素的边界或框中将x和y更改为适当的值。
左上方
let xOrigin = 0
中上层
let xOrigin = self.view.bounds.width / 2
右上方
let xOrigin = self.view.bounds.width
希望这会有所帮助。