gpt4 book ai didi

swift - GameScene 分享按钮

转载 作者:行者123 更新时间:2023-11-28 12:17:58 27 4
gpt4 key购买 nike

我的游戏已经准备就绪,我唯一缺少(并想添加)的是向社交媒体分享标准消息的选项。我环顾四周,发现了不同的方法来做到这一点,但当我实现它们时,它们似乎都不起作用。我有一种感觉,我做错了可能很简单!

这是我的按钮:

func createShareButton() {
shareButton = SKSpriteNode(imageNamed: "shareBtn")
shareButton.size = CGSize(width: 240, height: 55)
shareButton.position = CGPoint(x: self.frame.width / 2, y: (self.frame.height / 2) - 150)
shareButton.zPosition = 6
self.addChild(shareButton)
shareButton.run(SKAction.scale(to: 1.0, duration: 0.3))
}

这是按下按钮时的代码(在我的 GameScene.swift 文件中):

let vc = self.view?.window?.rootViewController
let myText = "share test message!"
let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [myText], applicationActivities: nil)
vc?.present(activityVC, animated: true, completion: nil)

当我点击按钮时,控制台出现错误:

Warning: Attempt to present <UIActivityViewController: 0x149d4e880> on <MyGame.initialVC: 0x149d0fe90> whose view is not in the window hierarchy!

有什么地方出错了吗?

提前致谢!

最佳答案

我两天前遇到了同样的问题。我用这个 stackoverflow answer 修复了它

我不断收到同样的错误,这是因为 rootViewController 不是层次结构中的当前顶级 viewController。当我想让 viewController 控制当前的 SKScene 时,它​​试图使用我的初始 viewController。

希望对你有帮助

    func shareGame() {
if var top = scene?.view?.window?.rootViewController {
while let presentedViewController = top.presentedViewController {
top = presentedViewController
}
let activityVC = UIActivityViewController(activityItems: ["I am playing Crashbox! Check it out."], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = view
top.present(activityVC, animated: true, completion: nil)
}
}

关于swift - GameScene 分享按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45821684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com