2014-12-29 14 views
6

Bu kod, bir UIViewController oturan var (XCode 6.1, iOS 8.1.1) kullanılarak çalışma zamanı hatası nasıl düzeltilirUIAlertController

[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); 
          } 
         }]; 

bunu çalıştırdığınızda, bu çalışma zamanı hatası alıyorum:

*** 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.'

Bu işi yapmak için ne yapmam gerekiyor? (SO ve Google'a baktım ve özel bir şey bulamadım). Bu konuda bana olsun herhangi bir yardım ...

GÜNCELLEME takdir Ben 3. taraf kodu olmadan yeniden yazdı; Bu kodu ekledi ve şimdi çalışıyor!

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

cevap

1

burada gitmek için değerli az bilgi var ... Size https://github.com/ryanmaxwell/UIAlertController-Blocks kullandığınız görünür

istisna değil değişiklikleri önerir, bu durumda standart UIAlertController, o kodun sürümü' kullanarak henüz kapsamaz ya da sizin parçanızda ekstra çalışma gerektiren bir kullanım durumu.

Bu üçüncü taraf kodunu hiç kullanmadım, ancak hızlı bir kontrol, dokümanlardaki herhangi bir "bunu yap" ifadesini göstermiyor. İlk tavsiyem, söz konusu görüşe temsilci yöntemini uygulamak ve ona ne istediğini vermek (popletin sunulduğu yer) olacaktır.

+0

Soruyu düzeltilmiş kod ile güncelledim. Yardımın için teşekkürler, Brad! Bunu takdir ediyorum. – SpokaneDude

7

Bir iPad'de iPhone kodunu çalıştırdığınız için aldığınız hata mesajı belirdi. Bir iPad'de kullanım için alertController popoverPresentationController öğesini ayarlamanız gerekir. Kaynak dikdörtgeni, özensiz boyut hesaplamaları olmadan da oluşturulabilir. Aşağıda, bir düğmeye bastığınızda kodu nasıl karşılayacağınızı gösteren tam bir yöntemdir. AlertController'ı istediğiniz gibi ayarladıktan sonra popoverPresentationController'ı alıp iPad ile kullanmak için ayarlayın. Aşağıdaki yöntemde, basılan düğme göndericidir. Bu yüzden göndericiyi bu düğmeye geri göndeririz, ardından dikdörtgeni ayarlamak için düğmeyi kullanın. Dağınık boyutların hesaplanması gerekmez. Şimdi, iPad'de kodu çalıştırırsanız, popover İptal düğmesini (iPhone'da görünmeyen) göstermez. Bu tasarım gereğidir. Apple UIPopoverController belgelerini görüntülerseniz, bunun dışına dokunarak popover'in iptal edildiğini görürsünüz.

- (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]; 
} 
İlgili konular