swift如何在switch语句中获取一个按钮

时间:2016-05-07 17:35:20

标签: ios swift button switch-statement pressed

我有一个小问题。现在,我想要一个按钮(buttonAclicked)谁可以显示标签,然后我想要其他按钮,在这种情况下,我只做了一个(btnThree),可以键入数字,直到该人按下一个按钮(“确定” “)这样做按钮无法在标签内输入。

现在我的问题是,当我按下“buttonAclicked”来显示标签,然后使用我的开关语句按另一个按钮(btnThree)时它会崩溃。我知道按钮(btnThree)正在工作但我无法将其放入switch语句中。

我无法让它打印出来,所以我知道它与switch语句有关,我也试图将“sender.tag”更改为“UIButton()”和其他一些东西

    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"
    }
    }

1 个答案:

答案 0 :(得分:0)

此行中的操作:

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

必须与您想要调用的方法的名称相匹配 - 这可能是此函数:

@IBAction func buttonAclicked(sender: UIButton)

所以将该行修改为:

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