Title: Add Target Not Working Swift 3 - UIButton inside a UIStackView
Tags: ios;uiview;uibutton;swift3
Question: I am trying to add a UIButton to a stackview programatically which is a part of custom UIView class. The issue is if I try to add a target with the selector from the Custom UIView class, the selector is never fired on tapping the button. But if i give it a selector from my UIViewController it works fine.
Can someone tell me what I am doing wrong here ?

```self.isUserInteractionEnabled = false;
    self.translatesAutoresizingMaskIntoConstraints = false;
    self.layer.borderWidth = 1.0;
    self.layer.borderColor = UIColor.black.cgColor;
    self.expandCollapseButton = UIButton(frame: CGRect.zero);
    self.expandCollapseButton!.addTarget(self, action: #selector(OMLiquidActionButton.expandButtonActionHandler(sender:)), for: .touchUpInside);

    self.mainContainer = UIStackView(frame:CGRect.zero);
    self.mainContainer?.isUserInteractionEnabled = true;
    self.mainContainer?.axis = .vertical;
    self.mainContainer?.distribution = .equalSpacing;
    //Add the maincontainer/stackview to the parentview

    //add the view to the container
    self.mainContainer?.addArrangedSubview(self.expandCollapseButton!);

    self.parentView?.addSubview(self.mainContainer!);
```

Please Note: Constraints are already defined for the view programatically and button exists as the topmost view.

Comment: Thanks for the suggesting it but it has nothing to do with the UIResponder class. I am not able to understand why I am not able to register a selector in same UIView class.

Comment: parentView = superView. Main Container = StackView. I already changed the as suggested above but still the problem exists

Comment: Why are you not using `addArrangedSubview`?

Comment: The view hierarchy is related to the responder chain. When you set it up in the view controller are you adding it to a stack view in this weird way?

Comment: Could you post a little more code? Which class is your code snipped from? Why is `isUserInteractionEnabled = false`? Why is the frame of the mainContainer set to zero?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:02/01/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><uiview><uibutton><swift3>', 'Title': 'Add Target Not Working Swift 3 - UIButton inside a UIStackView', 'ViewCount': 1617, 'id': 41986701, 'user_AboutMe': '<p>Enthusiast | Workaholic | Programmer By Choice | Open Source Lover | Code Geek | Workout Addict | Explorer</p>\n', 'user_AccountId': 181110.0, 'user_DisplayName': 'Akhilesh Sharma', 'user_Reputation': 1518.0, 'user_Views': 361.0, 'user_id': 414880.0}}