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



我有一个小问题。现在,我想要一个可以显示标签的按钮(buttonAclicked),然后我想要其他按钮,在这种情况下,我只做了一个(btnThree),它可以输入数字,直到用户按下一个名为("ok")的按钮,这样按钮就不能在标签内输入。

现在我的问题是,当我按下"buttonAclicked"来显示标签,然后使用switch语句按下另一个按钮(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"
    }
    }

此行中的操作:

    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

相关内容

  • 没有找到相关文章

最新更新