2015-11-26 5 views
7

mir begegnet ist, was aussieht wie eine Situation, die die meisten Menschen konfrontiert werden, wenn ein UIActivityViewController auf dem iPad zu präsentieren versuchen; es stürzt mit:UIActivityViewController auf dem iPad mit Sourceview oder barButtonItem Absturz

Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7fc4f2d87d00>) should have a non-nil sourceView or barButtonItem set before the presentation occurs. 

mein Code hier:

- (void)shareLeaflet 
{ 
    NSString *forwardedString = [[NSString alloc] initWithFormat:@"Check out this leaflet\n\n %@ \n\nself.theURLToShare]; 
    UIActivityViewController *activityViewController = nil; 

    if (IDIOM == IPAD) 
    { 
     NSLog(@"iPad"); 
     activityViewController.popoverPresentationController.sourceView = self.view; 
//  activityViewController.popoverPresentationController.sourceRect = self.frame; 
     [self presentViewController:activityViewController 
          animated:YES 
         completion:nil]; 

    } 
    else 
    { 
     NSLog(@"iPhone"); 
     activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil]; 
     [self presentViewController:activityViewController animated:YES completion:nil]; 


    } 

In meinem viewDidLoad, ich habe:

UIBarButtonItem *composeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self         action:@selector(shareLeaflet)]; 

    self.navigationItem.rightBarButtonItem = composeButton; 
} 

Diese Ansicht ein UIPageViewController ist, die einige Bilder präsentiert, und wenn die Wenn der Nutzer den Share-Button drückt, erwarte ich, dass das iOS 8-Style-Share-Popup erscheint. Genau das passiert auf dem iPhone, aber auf dem iPad stürzt es weiter ab. Das führte mich zu Überlauf Stapel, aber keine der Fragen (crash on showing UIPopOverPresentationController, iOS Crash: Terminating app due to uncaught exception reason: UIPopoverPresentationController should have a non-nil sourceView, UIWebViewTerminating app due to UIPopoverPresentationController, ios8 iPad uiwebview crashes while displaying popover when user taps drop down list HTML select tag, usw.) arbeitet für mich.

Ich habe alle Lösungen dort versucht, und ich bekomme nur recht, was mit diesem erforderlich ist.

Das ist, was ich versuche zu erreichen:

enter image description here Irgendwelche Gedanken auf, dies würde wirklich zu schätzen.

Antwort

14

Sie sind nicht die activityViewController auf iPad initialisiert, so dass es immer gleich Null sein wird.

Versuchen:

- (void)shareLeaflet 
{ 
    NSString *forwardedString = [[NSString alloc] initWithFormat:@"Check out this leaflet\n\n %@ \n\nself.theURLToShare]; 
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil]; 

    if (IDIOM == IPAD) 
    { 
     NSLog(@"iPad"); 
     activityViewController.popoverPresentationController.sourceView = self.view; 
//  activityViewController.popoverPresentationController.sourceRect = self.frame; 
     [self presentViewController:activityViewController 
          animated:YES 
         completion:nil]; 
    } 
    else 
    { 
     NSLog(@"iPhone"); 
     [self presentViewController:activityViewController 
          animated:YES 
         completion:nil]; 
    } 

Und dann, um es anzuzeigen, wie in dem Bild: (_shareBarButton ist die UIBarButtonItem, dass Sie die popover wollen aus anzuzeigen) können

- (void)shareLeaflet 
    { 
     NSString *forwardedString = [[NSString alloc] initWithFormat:@"Check out this leaflet\n\n %@ \n\nself.theURLToShare]; 
     UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:forwardedString, nil] applicationActivities:nil]; 

     if (IDIOM == IPAD) 
     { 
      NSLog(@"iPad"); 
      activityViewController.popoverPresentationController.sourceView = self.view; 
    //  activityViewController.popoverPresentationController.sourceRect = self.frame; 

      _popover = [[UIPopoverController alloc] initWithContentViewController:activityViewController]; 
      _popover.delegate = self; 
      [_popover presentPopoverFromBarButtonItem:_shareBarButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
     } 
     else 
     { 
      NSLog(@"iPhone"); 
      [self presentViewController:activityViewController 
           animated:YES 
          completion:nil]; 
     } 
+0

Hallo @JDx - ah Mann, das war einfach ! Entschuldigung für die einfache Frage, die wie ein Zauber wirkte! Vielen Dank! Aber um eine Nachfolgefrage zu stellen - kann ich kontrollieren, von welcher Seite das Popup kommt? Im Moment kommt es von oben links auf dem Bildschirm, aber der Button befindet sich tatsächlich oben rechts. – amitsbajaj

+0

Kein Problem! Ich glaube nicht, dass du das kannst, vielleicht könntest du stattdessen einen UiPopoverController benutzen? – JDx

+0

Dank JD - Ich versuche nur die UIActivityViewController zu aktivieren, wie das iOS 9.8 Teilen Blatt. Ich habe gerade die Frage aktualisiert, um ein Bild von Safari auf dem iPad zu enthalten - es kommt direkt unter diesem Knopf, aber meins erscheint auf der linken Seite, die seltsam ist – amitsbajaj

1

Sie haben soeben die popoverPresentationController gesetzt barButtonItem für iPad, zum Beispiel:

let activityViewController = UIActivityViewController(activityItems: ["Hello, world!", urlString], applicationActivities: nil) 

if UIDevice.current.userInterfaceIdiom == .pad { 
    activityViewController.popoverPresentationController?.barButtonItem = barButtonItem 
} 

self.present(activityViewController, animated: true) 
Verwandte Themen