1

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!

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a great answer. Thanks a lot! I changed UIImageView to UIView and also changed target to self and it works
2

Try change the tarjet and the event

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

into

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

Comments

1

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)

2 Comments

@bla0009 did you change itemView to self?
@anthu yes, and still nothing happens on button click
1
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");
}

1 Comment

change forControlEvents:.TouchDown to forControlEvents: .TouchUpInside
0

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

Comments

0

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....

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.