I have a controller with a Segment Control and two subviews
@IBOutlet weak var firstContainerView: UIView!
@IBOutlet weak var secondContainerView: UIView!
@IBAction func segmentControlChanged(sender: AnyObject) {
if myController.selectedSegmentIndex == firstIndex {
self.view.sendSubview(toBack: secondContainerView)
}
else {
self.view.sendSubview(toBack: firstContainerView)
}
}
The view controller for the first subview occasionally fires up an SVProgressHUD whilst working on a big task. If I change views, this can be hidden, or re-shown in the viewWillAppear
/ viewWillDisappear
functions
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if (isWaiting) {
SVProgressHUD.show()
...
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if (isWaiting) {
SVProgressHUD.dismiss()
}
}
However, view.sendSubView(toBack: firstContainerView)
does not fire viewWillDisappear
.
Is there an event which the SubView viewController can receive to notify it of the change?
EDIT
After a lot of searching I found a way to get the viewController for the containing subView in this answer. In my case it was considerably complicated by the fact that this contained a a UIPageViewContainer which was created programtically; however I was able to get the viewController of the current page by following this tutorial
So I added this to the outermost view controller
override func prepare(for segue: UIStoryboardSegue, identifier: SegueIdentifier, sender: Any?) {
switch identifier {
case .PagingVCSegue:
if let pvc = segue.destination as? PagingVCContainer {
self.pagingViewContainer = pvc
}
default: break
}
}
This to the paging container
func getCurView() -> firstContainerView? {
if (pagingVC?.viewControllers?.count)! > 0 {
return pagingVC?.viewControllers![0] as? firstContainerView
}
return nil
}
and was then able to call a method in the view to hide the SVProgressHUD.