Can't add UIButton Outlet to UIView

I'm only able to add an Action to my UIView subclass and not an Outlet. But I am able to add an Outlet to the parent controller that is holding the UIView.

fig 1

fig 2

2 answers

  • answered 2018-08-15 09:34 Unreal Developer

    You cannot actually add a IBOutlet of a button to the UIView subclass, because your UIView subclass doesn't own this object. You need to assign a subclass of UIButton to make an outlet, or use parent ViewController class, that own all the subviews

  • answered 2018-08-15 09:38 DeyaEldeen

    You can only add UIButtons to a UIView programatically, but there's no we to do it with an IBOutlet.

    let testView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
    let button = UIButton()