我有一段代码放在一个UIViewController中(Xcode 6.1,iOS 8.1.1):
[UIAlertController showActionSheetInViewController:self
withTitle:@"Test Action Sheet"
message:NSLocalizedString(@"Are you sure you want to delete ALL appointments?",nil)
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Yes"
otherButtonTitles:@[@"No"] // same as Cancel
tapBlock:^(UIAlertController *controller, UIAlertAction *action, NSInteger buttonIndex){
if (buttonIndex == UIAlertControllerBlocksCancelButtonIndex) {
NSLog(@"Cancel Tapped");
} else if (buttonIndex == UIAlertControllerBlocksDestructiveButtonIndex) {
NSLog(@"Delete Tapped");
} else if (buttonIndex >= UIAlertControllerBlocksFirstOtherButtonIndex) {
NSLog(@"Other Action Index %ld", (long)buttonIndex - UIAlertControllerBlocksFirstOtherButtonIndex);
}
}];
当我运行它时,我得到了这个运行时错误:
未捕获的异常 'NSGenericException',原因是:'您的应用程序已经呈现了一个UIAlertController (),其样式为UIAlertControllerStyleActionSheet。具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover。您必须通过警报控制器的popoverPresentationController提供此弹出窗口的位置信息。您必须提供sourceView和sourceRect或barButtonItem之一。如果在呈现警报控制器时不知道这些信息,则可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供它们。'
我需要做什么才能使它工作?(我已经查看了SO和Google,没有找到具体的解决办法)。非常感谢您的帮助...
更新: 我重新编写了它而没有使用第三方代码;添加了这段代码,现在它可以工作了!
UIAlertController * view= [UIAlertController
alertControllerWithTitle:@"My Title"
message:@"Select your Choice"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
[view addAction:ok];
[view addAction:cancel];
view.popoverPresentationController.sourceView = self.view;
view.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 1.0, 1.0);
[self presentViewController: view animated:YES completion:nil];