2014-12-29 16 views
6

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]; 

Trả lời

1

Có thông tin quý giá ít đi về đây ...

Dường như bạn đang sử dụng https://github.com/ryanmaxwell/UIAlertController-Blocks, không phải là tiêu chuẩn UIAlertController, trong trường hợp ngoại lệ cho thấy những thay đổi mà các phiên bản mã bạn' tái sử dụng không bao gồm chưa hoặc một trường hợp sử dụng mà đòi hỏi thêm công việc về phía bạn.

Tôi chưa bao giờ sử dụng mã của bên thứ ba này nhưng kiểm tra nhanh không hiển thị bất kỳ "làm điều này" rõ ràng trong tài liệu. Đề xuất ban đầu của tôi là triển khai phương thức đại biểu trên chế độ xem được đề cập và cung cấp cho nó những gì nó muốn - vị trí mà tại đó trình bày cửa sổ bật lên.

+0

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

7

Thông báo lỗi bạn nhận được xuất hiện vì bạn đã chạy mã iPhone trên iPad. Để sử dụng trên iPad, bạn phải đặt popoverPresentationController của alertController. Hình chữ nhật nguồn có thể được tạo ra mà không cần tính toán kích thước cẩu thả. Dưới đây, là một phương pháp hoàn chỉnh, cho thấy cách bạn sẽ gặp phải mã khi nhấn một nút. Sau khi thiết lập AlertController theo cách bạn muốn, bạn nhận được popoverPresentationController của nó và thiết lập nó để sử dụng với iPad. Trong phương thức bên dưới, nút được nhấn là người gửi. Vì vậy, chúng tôi đưa người gửi trở lại nút đó, sau đó sử dụng nút để đặt hình chữ nhật. Không có kích thước lộn xộn cần tính toán. Bây giờ, nếu bạn chạy mã trên iPad, cửa sổ bật lên sẽ không hiển thị nút Hủy, (xuất hiện trên iPhone). Đó là do thiết kế. Nếu bạn xem tài liệu Apple UIPopoverController, bạn thấy rằng cửa sổ bật lên bị hủy bằng cách nhấn vào bên ngoài nó.

- (IBAction)showImagePickerButtonTapped:(id)sender; 
{ 
    BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; 
    BOOL isPhotoLibraryAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]; 

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; 

    if (isCameraAvailable) { 
     [alertController addAction:[UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
      [self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera]; 
     }]]; 
    } 

    if (isPhotoLibraryAvailable) { 
     [alertController addAction:[UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
      [self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
     }]]; 
    } 

    // The following lines are needed for use with the iPad. 
    UIPopoverPresentationController *alertPopoverPresentationController = alertController.popoverPresentationController; 
    UIButton *imagePickerButton = (UIButton*)sender; 
    alertPopoverPresentationController.sourceRect = imagePickerButton.frame; 
    alertPopoverPresentationController.sourceView = self.view; 

    [self showDetailViewController:alertController sender:sender]; 
} 
Các vấn đề liên quan