-4

I'm a newbie coder. I need to create a counter button. For example, I create an 'A' button, when I press it, the value of A is 1 and if I pressed it again, the value goes to 2. And that count is for chart purpose. Can someone help me how to create this?

Larme
  • 24,190
  • 6
  • 51
  • 81
graydam
  • 209
  • 3
  • 10

1 Answers1

1

this code will help you simply.

        let butt=UIButton()
        butt.setTitle("A", forState: .Normal)
        butt.frame=CGRectMake(100, 100, 100, 100)
        butt.backgroundColor = UIColor.greenColor()
        butt.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(butt)

    var counter=0;
    func buttonAction(sender:UIButton){

        ++counter
        print(counter)

    }