Bob
Bob

Reputation: 8704

Swift adding button into view programatically with action

I am trying to add button to the programatically created view that I add to main view. I can't make action to work on a button. This is what I tried so far:

itemView = UIImageView(frame:CGRect(x:0, y:0, width:240, height:375))
itemView.backgroundColor = UIColor(red: 255.0/0.0, green: 255.0/0.0, blue: 255.0/0.0, alpha: 0.05)
itemView.contentMode = .Center

buttonConnect = UIButton(frame: CGRect(x:0, y:335, width:120, height:40))
buttonConnect.backgroundColor = UIColor(red: 0.8, green: 0.6, blue: 0.2, alpha: 1.0)
buttonConnect.setTitle("Connect", forState: .Normal)
buttonConnect.tag = 3
buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown)
itemView.addSubview(buttonConnect)

self.addSubview(itemView)

Method on button click looks like this:

func btnConnectTouched(sender:UIButton!)
{
    print("button connect touched")
}

Can someone advice what is the problem? Thanks!

Upvotes: 1

Views: 4061

Answers (6)

Avinash Jadhav
Avinash Jadhav

Reputation: 501

If you can make following two changes in your code, it will work.

  1. Replace UIImageView with UIView for itemView type and
  2. Replace self.addSubview(itemView) with self.view.addSubview(itemView)

Your final code should look like....

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var itemView = UIView(frame:CGRect(x:0, y:0, width:240, height:375))
    itemView.backgroundColor = UIColor(red: 255.0/0.0, green: 255.0/0.0, blue: 255.0/0.0, alpha: 0.05)
    itemView.contentMode = .Center

    var buttonConnect = UIButton(frame: CGRect(x:0, y:335, width:120, height:40))
    buttonConnect.backgroundColor = UIColor(red: 0.8, green: 0.6, blue: 0.2, alpha: 1.0)
    buttonConnect.setTitle("Connect", forState: .Normal)
    buttonConnect.tag = 3
    buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchDown)


    itemView.addSubview(buttonConnect)

    self.view.addSubview(itemView)

}

func btnConnectTouched(sender:UIButton!)
{
    print("button connect touched")
}

Try this one.... It's working for me....

Upvotes: 0

Muneeba
Muneeba

Reputation: 1776

I don't understand why you are adding a button inside a UIImageView. Even this is what you need, take a UIView and then put all other controls inside it. The reason your button is not clicking is that you have put it inside a UIImageView and for UIImageView , default value for userInteractionEnabled is false. so you need to enable it i.e.

 itemView.userInteractionEnabled = true;

Also set button target to 'self' rather than itemView.

Upvotes: 5

Nisha Salim
Nisha Salim

Reputation: 707

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let buttonConnect = UIButton()      
    buttonConnect = UIButton(frame: CGRect(x:0, y:335, width:120, height:40))
    buttonConnect.backgroundColor = UIColor(red: 0.8, green: 0.6, blue: 0.2, alpha: 1.0)
    buttonConnect.setTitle("Connect", forState: .Normal)
    buttonConnect.tag = 3
    buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents: .TouchUpInside)       
    self.view.addSubview(buttonConnect)
}

func btnConnectTouched(sender: UIButton!) {
     print("button connect touched");
}

Upvotes: 1

Graeme K
Graeme K

Reputation: 89

try buttonConnect.addTarget(itemView, action: "btnConnectTouched", forControlEvents:.TouchUpInside) without the : after "btnConnectTouched"

Upvotes: 0

Manuel
Manuel

Reputation: 1695

Try change the tarjet and the event

buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown)

into

buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:. TouchUpInside)

Upvotes: 2

tuledev
tuledev

Reputation: 10317

You are wrong at the target:

// buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown)
    // using
    buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchDown)
    // or buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchUpInside)

Upvotes: 1

Related Questions