菜鸟教程小白 发表于 2022-12-12 15:03:25

ios - PopOverViewController 中的 UIActivityViewController 在 Swift 中崩溃


                                            <p><p>我有一个用于共享按钮的 UIActivityViewController。对于 iPhone,我将它作为常规 UIActivityViewController,对于 iPad,它在 PopOverViewController 中。这是我的代码</p>

<pre><code>let textToShare = &#34;Check out this website!&#34;

if let myWebsite = NSURL(string: &#34;http://www.apple.com/&#34;) {
   let objectsToShare =
   let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

   if let popUpVC = activityVC.popoverPresentationController {
      popUpVC.permittedArrowDirections = .Any
      popUpVC.sourceRect = share.frame
   }
   self.view?.window?.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}
</code></pre>

<p>当我在 iPad 上按下共享按钮时,它会崩溃并显示 (lldb)。但是,当我从 View 中看到它时,它可以工作,但位置不正确。这是我目前使用的代码。</p>

<pre><code>popUpVC.sourceView = self.view
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试这个,你必须检查你当前正在运行的设备是否响应 popoverPresentationController 因为 <code>popoverPresentationController</code> 是 iOS 8 的新功能,并且会在 iOS 7 上崩溃。它在 iPhone 上也是 nil因为它只存在于 iPad 上的 <code>UIPopover</code> 中。</p>

<pre><code>let activityViewController = UIActivityViewController(activityItems: , applicationActivities: nil)

if activityViewController.respondsToSelector(&#34;popoverPresentationController&#34;) {
   // iOS8+
   view.presentViewController(activityViewController, animated: true, completion: nil)
   activityViewController.popoverPresentationController?.sourceView = view
} else {
   view.presentViewController(activityViewController, animated: true, completion: nil)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - PopOverViewController 中的 UIActivityViewController 在 Swift 中崩溃,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/29570249/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/29570249/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - PopOverViewController 中的 UIActivityViewController 在 Swift 中崩溃