I have looked all over for the answer to this question and can't seem to find it. How can I call a viewController method from swiftUI (e.g. on a button click)?
I have a viewcontroller that looks like this:
import Player
class PlayerViewController: UIViewController {
var player = Player()
func play() {
self.player.play()
}
}
And I have a wrapper that looks like this:
import SwiftUI
import AVFoundation
struct ProjectEditorPlayerBridge: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> PlayerViewController {
let player = PlayerViewController()
return player
}
func updateUIViewController(_ uiViewController: PlayerViewController, context: Context) {
}
typealias UIViewControllerType = PlayerViewController
}
I want to be able to use a button action in swiftUI and call the viewController play
method once. I have seen answers that suggest setting state/binding on the wrapper and calling the method in updateUIViewController, but when I do this I see it gets called multiple times, not just once.