2015-11-26 24 views
7

ile iPad'de kilitleniyor iPad'in üzerinde bir UIActivityViewController sunmaya çalışırken çoğu insanın karşılaştığı duruma benzeyen bir noktaya rastladım; onunla kilitleniyor: BuradaUIActivityViewController, sourceView veya barButtonItem

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

benim kod: Benim viewDidLoad yılında

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


    } 

, ben:

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

    self.navigationItem.rightBarButtonItem = composeButton; 
} 

Bu görüş bir bazı görüntülerin sergiliyor UIPageViewController ve ne zaman olduğunu kullanıcı paylaş düğmesine bastığında, iOS 8 stili paylaşım sayfasını pop-up'a bekliyorum. Bu iPhone'da tam olarak ne oluyor, ancak iPad'de, çökmeye devam ediyor. Bu bana Stack Overflow yol açtı, ama soruların hiçbiri (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, vb) benim için çalışır.

Buradaki tüm çözümleri denedim ve bununla ne gerekiyorsa onu aldım. Bu konuda

enter image description here Herhangi bir düşünce gerçekten takdir:

Bu benim ulaşmak için çalışıyorum budur.

cevap

14

iPad'de activityViewController'ı başlatmıyorsunuz, böylece her zaman sıfır olacaktır.

Dene:

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

Sonra resimdeki gibi göstermek için:

- (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

Merhaba @JDx - erkek ah, çok kolay oldu ! Kolay soru için üzgünüm, bu bir cazibe gibi çalıştı! Teşekkür ederim! Ama sadece bir takip sorusu sormak için - pop-up'ın hangi tarafından geleceğini kontrol edebilir miyim? Şu anda, ekranın sol üstünden geliyor, ancak düğme aslında sağ üstte. – amitsbajaj

+0

Sorun değil! Yapabileceğini sanmıyorum, belki bir UiPopoverController kullanabilirdin? – JDx

+0

Teşekkürler JD - Sadece iOS 8/9 Paylaşım sayfası gibi UIActivityViewController'ı etkinleştirmeye çalışıyorum. IPad'de Safari'den bir görüntü eklemek için bu soruyu yeni güncelledim - bu düğmenin hemen altında geliyor ama benimki – amitsbajaj

1

sadece popoverPresentationController ayarlayabilirsiniz (_shareBarButton Eğer popover görüntülemek istediğiniz UIBarButtonItem olan) 'nin barButtonItem iPad için, örneğin:

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

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

self.present(activityViewController, animated: true) 
İlgili konular