我想在遊戲過程中調用共享函數。我有一些代碼,我可以在我的GameViewController如何從GameScene調用viewController中的函數
func showTweetSheet() {
let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweetSheet.completionHandler = {
result in
switch result {
case SLComposeViewControllerResult.Cancelled:
//Add code to deal with it being cancelled
break
case SLComposeViewControllerResult.Done:
//Add code here to deal with it being completed
//Remember that dimissing the view is done for you, and sending the tweet to social media is automatic too. You could use this to give in game rewards?
break
}
}
tweetSheet.setInitialText("Test Twitter") //The default text in the tweet
tweetSheet.addImage(UIImage(named: "TestImage.png")) //Add an image if you like?
tweetSheet.addURL(NSURL(string: "http://twitter.com")) //A url which takes you into safari if tapped on
self.presentViewController(tweetSheet, animated: false, completion: {
//Optional completion statement
})
}
我還設置視圖控制器在我GameScene類...
var viewController: GameViewController!
使用...並設置scene.viewController自我我視圖控制器
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
scene.viewController? = self
}
}
然而,當我打電話,像這樣的功能...
viewController.showTweetSheet()
...從我的GameScene中,它給了我一個「發現無解包可選值時」錯誤。
我想我可能需要將scene.viewController設置爲稍後,但我不知道如何在viewController中執行此操作。
任何幫助將不勝感激。
使用NSNotifications向VC發送消息以運行方法。 – sangony
@sangony你可以發佈這個答案,因爲這已經回答了這個問題。謝謝。 –