快速消息发送到已解除分配的实例 – 每个 IBAction



每次我在 Swift 项目中向视图控制器添加@IBAction时,都会收到一个错误(使用 NSZombies):[Shaan_Singh.SkillsViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x7f936380c170 。为什么会这样?我要做的就是将 IBAction 连接到一个按钮。有什么想法吗?

法典:

@IBAction func doSomething(sender: AnyObject) {
    println("some")
}

下面是调用视图控制器的代码:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        let touch = touches.first as! UITouch
        let location = touch.locationInView(self.view)
        for var l = 0; l < self.view.layer.sublayers.count; l++ {
            let hitLayer = CGPathContainsPoint(self.view.layer.sublayers[l].path, nil, location, true)
            if hitLayer == true {
                // Play sound
                audioPlayer.play()
                // Configure animation
                let endShape = UIBezierPath(rect: CGRectMake(0, 0, self.screenWidth, self.screenHeight)).CGPath
                let animation = CABasicAnimation(keyPath: "path")
                animation.toValue = endShape
                animation.duration = 0.4
                animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
                // Fire animation
                if let shapeLayer = self.view.layer.sublayers[l] as? CALayer {
                    shapeLayer.zPosition = 1
                    shapeLayer.addAnimation(animation, forKey: animation.keyPath)
                }
                // Segue to view controller
                if l == 5 {
                    self.performSegueWithIdentifier("shape1", sender: self)
                } else if l == 1 {
                    self.performSegueWithIdentifier("shape2", sender: self)
                } else if l == 6 {
                    self.performSegueWithIdentifier("shape3", sender: self)
                } else if l == 0 {
                    self.performSegueWithIdentifier("shape4", sender: self)
                } else if l == 4 {
                    self.performSegueWithIdentifier("shape5", sender: self)
                } else if l == 3 {
                    self.performSegueWithIdentifier("shape6", sender: self)
                } else if l == 2 {
                    self.performSegueWithIdentifier("shape7", sender: self)
                }
            }
        }
    }

您需要提供更多信息。此按钮链接到的视图控制器是如何创建的?

不知何故,您的视图控制器正在被解除分配。

发布创建视图控制器并将其显示在屏幕上的代码,以及保存对视图控制器的引用的变量的声明。

我的猜测是,您创建一个视图控制器,将其视图安装为另一个视图控制器的子视图,然后返回,以便视图控制器从其视图下释放出来。(如果您不保留对视图控制器的强引用,它将被解除分配,从而导致您描述的问题。

最新更新