Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a UIButton programmatically

Tags:

I am trying to build UIViews programmatically. How do I get a UIButton with an action function in Swift?

The following code does not get any action:

let btn: UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(buttonPuzzle)

The following selector function is:

func buttonAction(sender: UIButton!) {
    var btnsendtag: UIButton = sender
}
like image 893
Bhuwan Sharma Avatar asked Apr 05 '15 07:04

Bhuwan Sharma


People also ask

How do I code a button in Swift?

To create a button with a string title you would start with code like this: Button("Button title") { print("Button tapped!") } Tip: The classic thing to do when you're learning a framework is to scatter print() calls around so you can see when things happen.

What is UIButton Swift?

A control that executes your custom code in response to user interactions.


3 Answers

You're just missing which UIButton this is. To compensate for this, change its tag property.
Here is you answer:

let btn: UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = 1               // change tag property
self.view.addSubview(btn) // add to view as subview

Swift 3.0

let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle(title: "Click Me", for: .normal)
btn.addTarget(self, action: #selector(buttonAction), forControlEvents: .touchUpInside)
btn.tag = 1               
self.view.addSubview(btn)

Here is an example selector function:

func buttonAction(sender: UIButton!) {
    var btnsendtag: UIButton = sender
    if btnsendtag.tag == 1 {            
        //do anything here
    }
}
like image 149
Want Query Avatar answered Oct 12 '22 01:10

Want Query


Using a tag is a fragile solution. You have a view and you are creating and adding the button to that view, you just need to keep a reference to it: For example

In your class, keep a reference to the button

var customButton: UIButton!

Create the button and set the reference

let btn = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = .greenColor()
btn.setTitle("Click Me", forState: .Normal)
btn.addTarget(self, action: #selector(MyClass.buttonAction), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
customButton = btn

Test against this instance in the action function

func buttonAction(sender: UIButton!) {
    guard sender == customButton else { return }

    // Do anything you actually want to do here
}
like image 32
Abizern Avatar answered Oct 12 '22 00:10

Abizern


You have to addSubview and tag on that btn.

like image 26
Laxman Ghimire Avatar answered Oct 12 '22 01:10

Laxman Ghimire