当我尝试呈现一个uiactivityviewcontroller或只是另一个viewcontroller时,我得到一个lldb错误而没有任何进一步的解释
编辑:
游戏从GameMenuScene开始,当点击游戏时,它将转移到GameScene:
class GameMenuScene: SKScene {
weak var weakGameVC: GameViewController?
if nodeAtPoint.name == "play"{
NSUserDefaults.standardUserDefaults().setInteger(score+1, forKey: "currentLevel")
NSUserDefaults.standardUserDefaults().synchronize()
self.removeAllActions()
self.removeAllChildren()
var scene1:SKScene = GameScene(size: self.size)
scene1.weakGameVC = self @@Updated : error: SKSCene does not have a member named "weakGameVC"
self.view?.presentScene(scene1)
}
}
GameScene:
这是GameViewController(你可以看到,第一个场景是GameMenuScene:
import UIKit
import SpriteKit
import AVFoundation
import Social
class GameViewController: UIViewController, UITextFieldDelegate{
weak var weakGameVC: GameViewController? @@Updated
var player:SKSpriteNode = SKSpriteNode()
var scene:GameMenuScene!
override func viewDidLoad() {
super.viewDidLoad()
let skView = view as SKView
skView.multipleTouchEnabled = false
scene = GameMenuScene(size: skView.bounds.size)
scene.weakGameVC = self @@Updated
//scene.scaleMode = SKSceneScaleMode.ResizeFill
skView.showsFPS = true
skView.showsNodeCount = true
skView.presentScene(scene)
}
func shareButton() {
var myShare = "aa"
let activityVC:UIActivityViewController = UIActivityViewController(activityItems: ["aa"], applicationActivities: nil)
presentViewController(activityVC, animated: true, completion: nil)
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
这里是GameScene,游戏玩法是:
import SpriteKit
import Social
class GameScene: SKScene, SKPhysicsContactDelegate {
weak var weakGameVC: GameViewController?
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location: CGPoint! = touch.locationInNode(self)
let nodeAtPoint = self.nodeAtPoint(location)
if (nodeAtPoint.name != nil) {
if nodeAtPoint.name == "share"{
println("1")
if let gvc = weakGameVC {
println("2")
gvc.shareButton()
}
}
}
}
}
}
答案 0 :(得分:0)
它不起作用,因为在touchBegan
方法中您创建了新对象GameViewController
:
var gameViewController = GameViewController()
gameViewController
未添加到视图层次结构中(未显示),之后您调用shareButton()
方法,该方法想要在未显示的视图(游戏视图)上显示另一个视图(共享视图)
解决方案应该是通过调用自shareButton()
上的shareButton()
,在gameViewController.shareButton()
的同一个班级中处理触摸(touchesBegan)。
或者从场景到视图控制器创建委托方法,如果在场景调用委托方法中发生触摸并在视图控制器中处理它以呈现共享视图
// EXTENDED - 对游戏视图控制器的弱引用的解决方案。
在GameScene中添加对gameVC的弱引用:
weak var weakGameVC: GameViewController?
以后在同一个文件中:
if nodeAtPoint.name == "share" {
if let gvc = weakGameVC {
gvc.shareButton()
}
}
在创建游戏场景后的GameViewController文件中,您必须添加类似的内容:
gameScene.weakGameVC = self