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?
Asked
Active
Viewed 5,324 times
-4
-
value of A ? do you mean button title ? – Teja Nandamuri Nov 16 '15 at 14:12
-
Do you want to do it using interface builder or all in code? – NSGangster Nov 16 '15 at 14:12
-
there are tons of exampls for this!!! DO some research before posting the quesiton!! – Teja Nandamuri Nov 16 '15 at 14:14
-
2Possible duplicate of [How to create a button programmatically?](http://stackoverflow.com/questions/24030348/how-to-create-a-button-programmatically) – NSNoob Nov 16 '15 at 14:14
1 Answers
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)
}

Hussein Alzand
- 169
- 3