ios - SpriteKit 中的分享按钮崩溃了?

标签 ios swift

所以我有一个共享按钮,它可以打开 UI-Activity。当我单击共享按钮时,它崩溃并收到以下错误:libc++abi.dylib:因未捕获的 NSException 类型异常而终止。任何人都可以检查我的代码并看看发生了什么事吗?谢谢 *最后一件事:当他们返回游戏时,共享按钮保留在屏幕上。我怎样才能隐藏它,比如只在游戏结束场景中显示它?

here is my code

    shareButton = UIButton(frame: CGRect(x: 0, y:0, width: view.frame.size.width / 3, height: 60))

//        shareButton.center = CGPoint(x: self.size.width/4, y: self.size.height/4)
        shareButton.center = CGPoint(x: view.frame.size.width / 2 - 70, y: view.frame.size.height / 1.275)
        shareButton.center = CGPoint(x: self.frame.midX, y: 3*self.frame.height/4)

        shareButton.setTitle("Share", for: UIControlState.normal)
        shareButton.setTitleColor(UIColor.white, for: UIControlState.normal)
       shareButton.addTarget(self, action: (#selector(GameOver.pressed(_:))), for: .touchUpInside)


//        shareButton.removeFromSuperview()
        self.view?.addSubview(shareButton) 



 func pressed(_ sender: UIButton!) {


 let va = self.view?.window?.rootViewController

 let myText = "Can you beat my score \(score) in the game of Balls"

 let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [myText], applicationActivities: nil)

 va?.present(activityVC, animated: true, completion: nil)




 }

最佳答案

如果函数 pressed: 位于 GameOver 类中,请勿将目标添加为 self。而是将目标添加为 GameOver

的对象
let objGameOver = // Get your existing GameOver class instance here
shareButton.addTarget(objGameOver, action: (#selector(GameOver.pressed(_:))), for: .touchUpInside)

关于ios - SpriteKit 中的分享按钮崩溃了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44841549/

相关文章:

swift - 以编程方式更改 Mac 光标速度

ios - 当有多种通知时,我如何拥有不同的 UIUserNotificationSettings

ios - 用新数据刷新同一个 View Controller ?

ios - 如何仅更改一个特定行的高度?

swift - 如何让 Sprite 在点击时改变方向?

swift - WKWebview 未显示远程 URL 中的正确字体

ios - 在 MKMapView 上动态(交互地)绘制动画

iphone - 在 iOS 中嵌套 UIViewController

ios - 从 Apple 登录注销

swift - "Self"在 switch 语句中工作,但在 Swift 中的 If 语句中不起作用