Swift消息发送到解除分配的实例 - 每个IBAction

时间:2015-04-21 02:26:10

标签: ios swift

每次我在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)
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

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

不知何故,您的视图控制器已被取消分配。

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

我的猜测是你创建了一个视图控制器,将它作为另一个视图控制器的子视图安装,然后返回,以便视图控制器从它的视图中释放出来。 (如果您没有对视图控制器进行强有力的引用,它将被解除分配,从而导致您描述的问题。