iosobjective-cpushviewcontrollerpoptoviewcontroller

viewWillAppear does not call in IOS 13 objective C


I have a large app that was created under Xcode 8 (so its old school). I am not using swift or storyboards, all controls are created in code.

Since moving to IOS 13 / Xcode 11.3, when a VC delegate is pushed onto the navigation controller, and then a popToRootViewController is invoked, the rootViewController viewWillAppear is not called.

I have tried to use

VC.modalPresentationStyle=UIModalPresentationFullScreen without any change in behavior.

I have a MainWindow.xib that contains a window and the navController is set as a subview

In AppDelegate.m :

mainVC = [[MainViewController alloc] init];
selectSomethingVC=[[SelectSomethingViewController] init];

navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
[window addSubview:navigationController.view];
[window setRootViewController:navigationController];        
[window makeKeyAndVisible];

In MainViewController

- (IBAction) selectSomethingButtonPressed:(id)sender
{
AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self.navigationController pushViewController:[del selectSomethingVC] animated:YES];
}

In SelectSomethingViewController

- (IBAction) cancelButtonPressed:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:true];
}

This code works fine on prior Xcode versions and IOS 10 and the code within viewWillAppear is run which is essential to the operation of the app. On earlier IOS and Xcode, both on simulators and actual devices.

With IOS 13 and Xcode 11.3, the simulators do not call viewWillAppear but ,oddly, works correctly on an actual device running IOS 13 and launched within Xcode as a test device. I am open to work arounds or suggestions.


Solution

  • Adding a controller's view manually doesn't make sense if you are not also adding a child controller's manually.

    The following line should be removed:

    [window addSubview:navigationController.view];
    

    viewWillAppear is tied to adding view to the hierarchy. When you display the controller using setRootViewController: the view is already part of the hierarchy, which probably breaks the event mechanism.