UGandhi
UGandhi

Reputation: 579

Button action is not working on subView

In one of my iOS project, I have added a subview for Filter ViewController(subview) on my Feed ViewController(main view) programmatically.

There are few button on Filter ViewController to select price, city etc.

The outlets are connected but when I am trying to shoot button action, it is not working. I have also enabled the isUserInteractionEnabled but still it is not working.

Acc. to me, this is something related to subview on a view !! but to resolve this. Can you suggest me how to shoot a button action of subview it happen ?

class FilterViewController: BaseUIViewController{

   override func viewWillAppear(_ animated: Bool) {
     super.viewWillAppear(animated)

     cityButton.isUserInteractionEnabled = true
   }

   @IBAction func selectCity(_ sender: Any)
   {
    print("selectCity action")
   }
}

Upvotes: 0

Views: 2740

Answers (2)

Dileep
Dileep

Reputation: 2425

@SandeepBhandari solution is still valid for similar issues.

These UIViewController extensions will be useful.

extension UIViewController {

func add(asChildViewController viewController: UIViewController, containerView: UIView) {
    addChild(viewController)
    containerView.addSubview(viewController.view)
    viewController.view.frame = containerView.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    viewController.didMove(toParent: self)
}

func remove(asChildViewController viewController: UIViewController) {
    viewController.willMove(toParent: nil)
    viewController.view.removeFromSuperview()
    viewController.removeFromParent()
} 

}

Upvotes: 0

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20369

Add your FilterViewController as subview to your ViewController using the code below

filterViewController.willMove(toParentViewController: self)
self.view.addSubview(filterViewController.view)
self.addChildViewController(filterViewController)
filterViewController.didMove(toParentViewController: self)

Upvotes: 5

Related Questions