iosswiftbuttonswitch-statementpressed

swift how to get a button inside a switch statement


I have a slight problem. Now, i wanna have a button(buttonAclicked) who can show a label and then i wanna have other buttons, in this case i have only made one(btnThree), which can type in numbers until the person press a button called ("ok") which does so the buttons can't type inside the label.

Now my problem is that when i press the "buttonAclicked" to show the label and then press another button(btnThree) using my switch-statement it crashes. I know that the button(btnThree) is working but i can't fit it into the switch-statement.

i can't get it to print the line out so i know it has something to do with the switch-statement, i have also tried to change the "sender.tag" to "UIButton()" and some other things.

    class TriangleViewController : UIViewController {

    @IBOutlet var btnThreeclicked : UIButton!
    @IBOutlet var buttona : UIButton!

         }

     override func viewDidLoad() {
                  Open.target = self.revealViewController()
                  Open.action = Selector("revealToggle:")




          let btnThree = UIButton()
            btnThree.frame = CGRectMake(0.52 * view.bounds.width, 0.82 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height)  //set frame
            btnThree.setTitle("3", forState: .Normal)  //set button title
            btnThree.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
            btnThree.backgroundColor = UIColor.darkGrayColor() //set button background color
            btnThree.tag = 3 // set button tag
            btnThree.addTarget(self, action: "btnThreeclicked:", forControlEvents: .TouchUpInside) //add button action
            self.view.addSubview(btnThree) //add button in view



let buttona = UIButton()
        buttona.frame = CGRectMake(0.772 * view.bounds.width, 0.32 * view.bounds.height, 22, 22)
        buttona.layer.cornerRadius = 0.04 * view.bounds.width
        buttona.backgroundColor = UIColor.greenColor()
        buttona.setImage(UIImage(named:"A.png"), forState: .Normal)
        buttona.addTarget(self, action: "buttonAclicked:", forControlEvents: .TouchUpInside)
        self.view.addSubview(buttona)
    }




        @IBAction func buttonAclicked(sender: UIButton)
    {
            print("button A was pressed")

                label.hidden = false
                label.setNeedsDisplay()




      switch sender.tag{
       case 3 :

            print("buuuutttttooonnn")
        break



        default :
            label.text = "0"
    }
    }

Solution

  • The action in this line:

        btnThree.addTarget(self, action: "btnThreeclicked:", forControlEvents: .TouchUpInside) //add button action
    

    must match the name of the method you want called - which is presumably this function:

    @IBAction func buttonAclicked(sender: UIButton)
    

    so amend the line to:

        btnThree.addTarget(self, action: "buttonAclicked:", forControlEvents: .TouchUpInside) //add button action