Determine when a view becomes active

Hi,

There are two view involved here. For clarity call them VC1 and popup VC.


The popup VC is presented as a popover segue from VC1 and the segue is defined in the IB.


What I want to do is when the popup VC is dismissed I need to trigger the tableView array to be re-built and the tableView to be reloaded on VC1. I know how to do tableview work. However, viewDidAppear, viewDidLoad, viewWillAppear, etc. obviously don't run in this circumstance. What I haven't been able to figure out is how to determine when VC1 becomes active again in order to run the necessary tableView code.


Thanks in advance.


The only additional code for the segue is below.


override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == "tally_pop"
        {
            let popupVC = segue.destination as! Tallly_Popover_VC
            popupVC.preferredContentSize = CGSize(width: 300, height: 462)
        }
    }
Answered by PBK in 422173022

(I don't do Swift or Storyboards...)

A simple solution is a postNotificationName in viewWillDisappear (or the action that causes the view to dismiss itself) of the child and an addObserver: in the parent.

//in child
   [[NSNotificationCenter defaultCenter] postNotificationName:@"ClosingChild" object:self];


//in parent
   [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(refreshTableMethod)
                name:@"ClosingChild" object:nil];

Effectively, they don't get called because the parent view (VC1) is already there.


You have several options:

- use UIAdaptivePresentationControllerDelegate

h ttps://sarunw.com/posts/modality-changes-in-ios13/

or at the end of:

h ttps://medium.com/@hacknicity/view-controller-presentation-changes-in-ios-13-ac8c901ebc4e


- use didMove(toParent:)

https://stackoverflow.com/questions/57952783/uinavigationcontroller-swipe-down-gesture-detect


- maybe you could use in VC1 didBecomeKeyNotification

https://developer.apple.com/documentation/uikit/uiwindow/1621607-didbecomekeynotification

Accepted Answer

(I don't do Swift or Storyboards...)

A simple solution is a postNotificationName in viewWillDisappear (or the action that causes the view to dismiss itself) of the child and an addObserver: in the parent.

//in child
   [[NSNotificationCenter defaultCenter] postNotificationName:@"ClosingChild" object:self];


//in parent
   [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(refreshTableMethod)
                name:@"ClosingChild" object:nil];

Thanks PBK,

I tried at least 6 ways of doing this but this was the only one the worked.

I had to figure out a little more about NotificationCenter than I originally knew but I got it working.

Even figured out how to pass data to the parrent VC in the Notification


// In parrent
static let notificationName = Notification.Name("ClosingChild")

override func viewDidLoad()
{
   super.viewDidLoad()

   NotificationCenter.default.addObserver(self, selector: #selector(returnFromPopup), name: Tally_VC.notificationName, object: nil)
       
}// End of viewDidLoad
   
    @objc func returnFromPopup(notification: Notification)
    {
        if let data = notification.userInfo?["data"] as? Bool
        {
            if data
            {
                self.populateTheData()
                self.tableView.reloadData()
            } else {
                //print("It didn't run")
            }
        }
    }

// In child
var pass_Popup: Bool = false

override func viewDidDisappear(_ animated: Bool)
{
   super.viewDidDisappear(animated)
       
   setThe_Pass_Bool()
                               
   NotificationCenter.default.post(name: Tally_VC.notificationName, object: nil, userInfo: ["data": pass_Popup])
}

This is a powerful tool for getting an object from one Controller to any other Controller. It violates Model-View-Controller structure so use it wisely.

Determine when a view becomes active
 
 
Q