I have an HMSegmentedControl used for switching between segments. I used container view to switch between these segments, which works fine, but one of my tabs have a video that is autoplayed on viewDidAppear. So my problem is that as container view loads everything prior and shows views depending on isHidden = false, my video starts playing even if that segment isn't selected. How can I go about this situation?
This is my code on segmentedControlValueChanged event
print("selected index \(segmentedControl.selectedSegmentIndex)")
switch segmentedControl.selectedSegmentIndex {
case 0:
liveContainer.isHidden = true
case 1:
liveContainer.isHidden = true
case 2:
liveContainer.isHidden = false
default:
break
}
You can use
NSNotificationCenterto send notification to the view controller that contains the video when it is shown/hidden to play/stop the video. This can be done in the container view segment selection. So, you can remove autoplay fromviewDidAppearand add it to the method called when the notification is sent.For example, in your
segmentedControlValueChangedevent, you can write:and in your video
ViewController, you can have two methods: one for playing video:and another for stopping it:
and in your video
ViewController'sviewDidLoadmethod, you can add observers: