UIActionSheet在iPad上迅速崩溃

时间:2015-06-27 18:43:20

标签: ios swift ipad

我在工具栏中有以下按钮操作:

  @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并且没有修复它。谁能解释一下?

1 个答案:

答案 0 :(得分:1)

在iPad上,操作表是一个弹出窗口。因此,必须为其UIPopoverPresentationController提供sourceViewsourceRectbarButtonItem,以便它可以附加箭头。