簡體   English   中英

您如何在SKScene中顯示視圖控制器?

[英]How do you present a view controller in a SKScene?

我試圖弄清楚如何在SpriteKit游戲中使用共享表,並且當我嘗試從場景中呈現視圖控制器時,它給了我一個錯誤。

if self.nodeAtPoint(location) == self.shareButton {
        var tempMessage = "I just got \(score) on [insert app]"

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

        self.presentViewController(activityVC, animated: true, completion: nil)
}

這是在我的GameOverScene中。 由於我看到的答案是在OBJ-C中,因此您如何在Swift中管理視圖控制器。

謝謝!

好的,所以我做了一些挖掘,找到了可行的解決方案。

基本上,我使用NSNoticationCenter並在GameViewController文件中設置了視圖控制器。

override func viewDidLoad() {
.
.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showSocialNetworks", name: "showSocialNetworksID", object: nil)
}

並使函數被調用

func showSocialNetworks(){
    var tempMessage = "Insert your message to be shared on social network"

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

    self.presentViewController(activityVC, animated: true, completion: nil)
}

現在跳回到我的GameOverScene,我進行了設置,因此當用戶點擊“共享”按鈕時,它將執行以下代碼

if self.nodeAtPoint(location) == self.shareButton {
             NSNotificationCenter.defaultCenter().postNotificationName("showSocialNetworksID",  object: nil)

}

這可能不是最干凈的方法,但是對我有用。 希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM