-1

我有一个小问题。现在,我想要一个可以显示标签的按钮(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"
    }
    }
4

1 回答 1

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
于 2016-05-07T18:20:01.687 回答