2015-06-27 65 views
0

我有一個工具欄下面的按鈕操作:UIActionSheet崩潰迅速

@IBAction func share(sender: AnyObject) { 
     let modifiedURL1 = "http://www.declassifiedandratified.com/search.html?q=\(self.searchBar.text)" 
     let modifiedURL = modifiedURL1.stringByReplacingOccurrencesOfString(" ", withString: "%20", options: NSStringCompareOptions.LiteralSearch, range: nil) 

     let alert = UIAlertController(title: "Share", message: "Share your findings", preferredStyle: UIAlertControllerStyle.ActionSheet) 
     let twBtn = UIAlertAction(title: "Twitter", style: UIAlertActionStyle.Default) { (alert) -> Void in 
      if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){ 
       var twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter) 
       twitterSheet.setInitialText("Look what I found on Declassified and Ratified: \(modifiedURL)") 
       self.presentViewController(twitterSheet, animated: true, completion: nil) 
      } else { 
       var alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.Alert) 
       alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
       self.presentViewController(alert, animated: true, completion: nil) 
      } 
     } 

     let fbBtn = UIAlertAction(title: "Facebook", style: UIAlertActionStyle.Default) { (alert) -> Void in 
      if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){ 
       var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook) 
       facebookSheet.setInitialText("Look what I found on Declassified and Ratified: \(modifiedURL)") 
       self.presentViewController(facebookSheet, animated: true, completion: nil) 
      } else { 
       var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert) 
       alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
       self.presentViewController(alert, animated: true, completion: nil) 
      } 
     } 
     let safariBtn = UIAlertAction(title: "Open in Safari", style: UIAlertActionStyle.Default) { (alert) -> Void in 
      let URL = NSURL(string: modifiedURL) 
      UIApplication.sharedApplication().openURL(URL!) 
     } 
     let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alert) -> Void in 
      println("Cancel Pressed") 
     } 

     let textBtn = UIAlertAction(title: "Message", style: UIAlertActionStyle.Default) { (alert) -> Void in 
      if (MFMessageComposeViewController.canSendText()) { 
       let controller = MFMessageComposeViewController() 
       controller.body = "Look what I found on Declassified and Ratified: \(modifiedURL)" 
       controller.messageComposeDelegate = self 
       self.presentViewController(controller, animated: true, completion: nil) 
      } 
     } 


     alert.addAction(twBtn) 
     alert.addAction(fbBtn) 
     alert.addAction(safariBtn) 
     alert.addAction(textBtn) 
     alert.addAction(cancelButton) 
     self.presentViewController(alert, animated: true, completion: nil) 
    } 

但是,此代碼崩潰時與一個sigbart崩潰iPad的調用(這是所有我可以在控制檯中看到) 。我發現這是一個普遍的問題,但其他解決方案並沒有爲我工作。我甚至將版本設置爲最新的iOS,但沒有解決它。有人可以解釋嗎?

+0

發佈符號化的崩潰日誌。如果它在模擬器中崩潰,請指出它崩潰的線路。 –

+0

@RoboticCat它在模擬器上墜毀,但沒有給我一個行 – modesitt

+0

你應該更擔心的是,爲什麼你不能看到錯誤消息,它說「UIPopoverPresentationController應該有一個非空sourceView或barButtonItem設置的演示文稿之前發生」。你沒有看到它的事實意味着你對Xcode的理解有一些漏洞。這是一個非常非常有用的錯誤消息;它會告訴你到底發生了什麼問題。但是你需要學習如何看待它。 – matt

回答

1

在iPad,動作片是酥料餅。因此,你必須其UIPopoverPresentationController一個sourceViewsourceRect,或barButtonItem,使之具有一些附加的箭頭。

+0

所以這樣的:如果讓popoverController = alertController.popoverPresentationController { popoverController.barButtonItem =發件人 } – modesitt