Tôi đã mã này ngồi trong một UIViewController (XCode 6.1, iOS 8.1.1):Làm thế nào để khắc phục lỗi run-time sử dụng UIAlertController
[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);
}
}];
Khi tôi chạy nó, tôi nhận được lỗi run-time này:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7fdfe3324f00>) 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.'
Tôi cần phải làm gì để thực hiện công việc này? (Tôi đã xem xét SO và Google và không tìm thấy gì cụ thể). Tôi đánh giá cao bất kỳ trợ giúp nào tôi có thể nhận được về việc này ...
CẬP NHẬT Tôi đã viết lại nó mà không có mã của bên thứ ba; thêm mã này, và bây giờ nó hoạt động!
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];
Tôi đã cập nhật câu hỏi bằng mã đã sửa. Cảm ơn sự giúp đỡ của bạn, Brad! Tôi đánh giá cao nó. – SpokaneDude