Something like prepareForSegue, but on return

In my iOS application, I use UINavigationController with segues setting in Xcode. For one of the views I want to hide the navigation bar, and for the rest I want to show it. I can successfully hide the panel in the main window and then animate it nicely when I move to the next view, but when I return (using the back button in the navigation panel), the panel simply disappears, leaving a black rectangle, then switches to the previous view. I would like to be able to catch this, with something like the opposite of prepareForSegue, and beautifully animate the navigation bar. Is there any way to do this?

+4
source share
2 answers

There is currently no prepareForDesegue:sender: alternative to prepareForSegue:sender: Recommended practice is to set the link at the ViewController back to the ViewController source. Then, when the destination of the ViewController rejected, it can notify the source of the ViewController that it will again become the top ViewController . Typically, the link is set to prepareForSegue:sender:

So, to make it specific, suppose you have ViewControllerA , and just about move on to ViewControllerB . In ViewControllerB you must define a property that references ViewControllerA . (This is often done using protocols, but to make it simple, just assume that ViewControllerB has @property ViewControllerA *delegate; ) Then in prepareForSegue:sender: you will do the following:

 ViewControllerB * vcB = (ViewControllerB *)[segue destinationViewController]; vcB.delegate = self; 

Later, in ViewControllerB , in any code that returns you to ViewControllerA , you should use self.delegate to return to ViewControllerA , and tell it that it should be presented, and give it the opportunity to do whatever you need, using the UINavigationBar .

+2
source

In the UIViewController that you want the navigation bar to display, put the following methods:

 - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.navigationController setNavigationBarHidden:YES animated:animated]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:NO animated:animated]; } 

You can add some logic if you want the panel to freeze for some reason (for example, some of the following views still need the panel).

0
source

Source: https://habr.com/ru/post/1396059/


All Articles