Fred J.
Fred J.

Reputation: 6049

container view calls parent func in swift

In the storyBoard "parent" scene with its parentVC.swift, there is a containerView with embedded segue with its containerVC.swift.
I am able to call containerView.myFunc() in the parentVC viewDidLoad() with no problems.
How can I call a custom func defined in parentView from an action of a button in containerView.

self.parentViewController.myCustomFunc()

I get

Value of type UIViewController? has no member myCustomFunc

Upvotes: 0

Views: 1090

Answers (1)

Phillip Mills
Phillip Mills

Reputation: 31026

You need to cast parentViewController so the compiler knows which functions are available. The definition is simply UIViewController? which, as the error says, doesn't have your function.

Try:

if let vc = self.parentViewController as? parentVC {
    vc.myCustomFunc()
}

Upvotes: 1

Related Questions