I have a uibutton in child view controller that its action needs to be called in parent view controller. this is my code
class ChildViewController: UIViewController {
@IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
btn.addTarget(nil, action: Selector(("onBtnTap")), for: .touchUpInside)
}
}
class ParrentViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func onBtnTap(sender: Any) {
print("Hey, I am Parent VC ")
}
}
when i tap button the method onBtnTap not called and no crash occurred .
You can create an instance of Parent in Child to achieve this, here's how:
class ChildViewController: UIViewController {
@IBOutlet weak var btn: UIButton!
let parentVC = ParentViewController()
override func viewDidLoad() {
super.viewDidLoad()
btn.addTarget(parentVC, action: #selector(parentVC.onBtnTap(sender:)), for: .touchUpInside)
}
}
class ParentViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@objc func onBtnTap(sender: Any) {
print("Hey, I am Parent VC ")
}
}