UIButton帮助Swift

时间:2014-09-16 00:57:02

标签: objective-c swift uibutton

我试图编写一个执行该操作的UIButton,将消息输出到控制台。我可以看到按钮并触摸它但是只要我触摸它我的应用程序就会停止运行并且控制台显示以下错误:

Game[2743:63724] -[Game.GameScene buttonAction:]: unrecognized selector sent to instance 0x7a11ad00
2014-09-15 17:51:06.106 Game[2743:63724] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GameGameScene buttonAction:]: unrecognized selector sent to instance 0x7a11ad00' 

这是我的代码:

    let imageButton = UIImage(named: "playbutton2") as UIImage
    let buttonPlay = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    buttonPlay.frame = CGRectMake(screenSize.width / 2 , screenSize.height / 2, 100, 100)
    buttonPlay.setImage(imageButton, forState: .Normal)
    buttonPlay.addTarget(self, action: Selector("buttonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
    buttonPlay.tag = 22
    self.view.addSubview(buttonPlay)

    func buttonAction(sender:UIButton!) {
        println("Button tapped") 
    }

-------完整代码---------

import SpriteKit

class GameScene: SKScene {

    var skyColor = SKColor()
    var bird = SKSpriteNode()
    var moving = SKNode()
    var titleLabelNode = SKLabelNode()
    var title = NSString()
    let imageButton = UIImage(named: "playbutton2") as UIImage

    let screenSize: CGRect = UIScreen.mainScreen().bounds



    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        self.addChild(moving)
        skyColor = SKColor(red: 113.0/255.0, green: 197.0/255.0, blue: 207.0/255.0, alpha: 1.0)
        self.backgroundColor = skyColor

        var birdTexture1 = SKTexture(imageNamed: "Bird1")
        birdTexture1.filteringMode = SKTextureFilteringMode.Nearest

        var birdTexture2 = SKTexture(imageNamed: "Bird2")
        birdTexture2.filteringMode = SKTextureFilteringMode.Nearest

        var animation = SKAction.animateWithTextures([birdTexture1, birdTexture2], timePerFrame: 0.2)
        var flap = SKAction.repeatActionForever(animation)

        bird = SKSpriteNode(texture: birdTexture1)
        bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) + 150)
        bird.runAction(flap)

        bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height/2.0)
        bird.physicsBody.dynamic = false
        self.addChild(bird)

        var groundTexture = SKTexture(imageNamed: "Ground")
        groundTexture.filteringMode = SKTextureFilteringMode.Nearest

        var moveGroundSprite = SKAction.moveByX(-groundTexture.size().width, y: 0, duration: NSTimeInterval(0.01 * groundTexture.size().width))
        var resetGroundSprite = SKAction.moveByX(groundTexture.size().width, y: 0, duration: 0)
        var moveGroundSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite, resetGroundSprite]))
        for var i:CGFloat = 0; i<2 + self.frame.size.width / (groundTexture.size().width); ++i {
            var sprite = SKSpriteNode(texture: groundTexture)
            sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2)
            sprite.runAction(moveGroundSpritesForever)
            moving.addChild(sprite)


        }
        title = "Swing Bird"

        titleLabelNode.fontName = "Helvetica-Bold"
        titleLabelNode.fontSize = 80
        //titleLabelNode.fontColor = SKColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.)
        titleLabelNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + 50)
        titleLabelNode.text = "\(title)"
        addChild(titleLabelNode)




        let buttonPlay = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        buttonPlay.frame = CGRectMake(screenSize.width / 2 , screenSize.height / 2, 100, 100)
        buttonPlay.setImage(imageButton, forState: .Normal)
        buttonPlay.addTarget(self, action: Selector("buttonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
        buttonPlay.tag = 22
        self.view.addSubview(buttonPlay)


        func buttonAction(sender:UIButton!) {



            println("Button tapped")

        }

    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

           }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

1 个答案:

答案 0 :(得分:1)

您的函数buttonAction定义在错误的级别。它目前是内部 didMoveToView的函数,而不是类GameScene的函数。

你可以这样解决:

class GameScene: SKScene {

    ...

    // Move the function here!
    func buttonAction(sender:UIButton!) {



            println("Button tapped")

    }

    override func didMoveToView(view: SKView) {
         ...