【问题标题】:error in viewcontroller (lldb) when presenting another view呈现另一个视图时视图控制器 (lldb) 中的错误
【发布时间】:2014-12-22 14:23:53
【问题描述】:

当我尝试展示一个 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)
            }
}

游戏场景:

这是 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()
                }
}
}
}
}
}

【问题讨论】:

  • 这是一个断点,你必须禁用它或删除它。
  • 不,我停用了断点
  • 当我在我的 Iphone 上尝试这个时,应用程序也崩溃了
  • 您能在应用崩溃后发布 Xcode 的屏幕截图吗?

标签: swift uiviewcontroller sprite-kit uiactivityviewcontroller


【解决方案1】:

它不起作用,因为在touchBegan 方法中您创建了新对象GameViewController

var gameViewController = GameViewController()

gameViewController 未添加到视图层次结构中(未显示),之后您调用 shareButton() 方法,该方法想要在未显示的视图(游戏视图)上显示另一个视图(共享视图)。

解决方案应该是通过调用 shareButton() 而不是 gameViewController.shareButton() 在拥有 shareButton() 的同一类中处理触摸 (touchesBegan)。 或者创建从场景到视图控制器的委托方法,如果在场景调用委托方法中发生触摸并在视图控制器中处理它以呈现共享视图

// EXTENDED - 弱引用游戏视图控制器的解决方案。

在 GameScene 中添加对 gameVC 的弱引用:

weak var weakGameVC: GameViewController?

以后在同一个文件中:

if nodeAtPoint.name == "share" {
   if let gvc = weakGameVC {
      gvc.shareButton()
   }
}

在创建游戏场景后的 GameViewController 文件中,您必须添加类似的内容:

gameScene.weakGameVC = self

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 2014-12-12
  • 2015-06-17
  • 1970-01-01
  • 2016-11-20
  • 2019-09-11
  • 1970-01-01
相关资源
最近更新 更多