UINavigationController - Run Code Before Popping View Controller - iphone

I have a stack of UIViewController subclasses. Each modifies a NSManagedObject model. Many of them also present their own modal view controllers.
I need to save changes to the NSManagedObjectContext when a user either 'pops' the view controller or pushes the next view controller.
Currently, I'm hiding the default back button and setting my own UIBarButtonItem with a target of self and a custom action.
This works okay, but ideally, I want to use the default back button and run code before the pop. Is there a way I can run my own code before the pop?
(I'd prefer not to put code into viewWillDisappear as persisting to disk can be expensive and this method can also be triggered by modals being displayed by view controller.) Can it be done?

You can do it in viewDidDisappear, after checking that self is either 1) the second last element in self.navigationController.viewControllers (the case where the next VC just got pushed) or 2) self.navigationController is nil (the self VC just got popped).

Yes.. Navigation controller has a delegate which indicates when a view controller popped or pushed.. You can use that to do your task...

Add following method in your code:
- (void) viewWillDisappear:(BOOL)animated{
//your code here
}

I use viewWillDissappear to make any changes persistant.
If required i Use viewWillAppear to recoginze any changes (reload the data) that may have taken place while other puhed view controlers did their work.

For pop check isMovingFromParent in viewWillDisappear
func viewWillDisappear(_ animation:Bool){
super.viewWillDisappear(animation);
if isMovingFromParent {
// your code here
}
}

Related

General clarification on viewWillAppear?

I have a general question about viewWillAppear, I pushed newly a view consider view1, now viewWillAppear is called for that method, if view1 pushed another view (view2). Now if view2 is popped view1's viewWillAppear is called again, how can I come to know in which case its called i.e (is it because of newly pushing or popping of other view).
Hope my question is understandable :)
TIA
Frankly, I do not know how to determine that condition appropriately.
You could of course set some property of view1 in view2's viewWillDisappear method and re-set that in view1's viewWillAppear. Such like:
view1.m:
- (void) viewWillAppear ... {
...
if (self.wasPopped) {...}
self.wasPopped = NO;
...
}
view2.m:
- (void) viewWillDisappear {
view1.wasPopped = YES; //you would have to have a reference to view1 or fetch it from the navigation controller stack.
}
However, I strongly believe that this is not really the way you should go. Consider moving your code to the viewDidLoad method. viewDidLoad is called only once when the view(Controller) is created well before viewWillAppear is called for the first time.
Depending on your context, you want to use...
For viewWillAppear/viewDidAppear
isBeingPresented
isMovingToParentViewController
For viewWillDisappear/viewDidDisappear
isBeingDismissed
isMovingFromParentViewController
Just simply remember that whenever your view will appear on screen(it is going to be visible ) viewWillAppear method will be called.
In your case when view2 is popped again your view1 is going to appear on the screen that's why it's viewWillAppear is called again. Same way when you push new view controller, that new view controller will be displayed on the screen. Before displaying that view, that new view controller's viewWillAppear will be called.
I hope it makes sense.
You can use BOOL value and initialize it to FALSE.In viewWillAppear make that bool value TRUE and in viewWillAppear only check whether that bool value is TRUE or FALSE if it is true means the view is appearing for second time.

Unbalanced calls to begin/end appearance transitions for <FirstViewController: 0x2a2c00>

I have this problem when I simulate my app, its not an error or a warning but it appears in my console, has anyone ever experienced this before?
In my case, this error occurs when you click two tabs in a tableview very fast.
The result causes wrong titlename, back button disappear. Someone mentioned that when you push a view, set animated:NO. The error will disappear but still causes some strange behavior. It pushes two views, then you need to back twice to get back the tableview screen.
Method I tried in order to resolve this problem:
add BOOL cellSelected;
in viewWillAppear cellSelected = YES;
in didselectcell delegate if (cellSelected){cellSelected = NO; do action ; }
This helps prevent clicking two different cells very fast.
In my case it happened when I triggered [self performSegueWithIdentifier:#"SomeIdentifier" sender:self]; within a UINavigationController item's viewDidLoad method.
Moving it into the viewDidAppear method solved the problem.
The reason very likely is that in viewDidLoad not all of the fancy animations have already been finished, whereas in viewDidAppear everything's done.
I have this problem too. I found two solutions to this problem:
You can see this solution above.
I found subclass from UINavigationController where this problem resolved. Buffered Navigation Controller
You should run your code in different loop to avoid this
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// Put your code here
[self presentViewController:self.yourModalVC animated:YES completion:nil];
});
I had lot of problem with the same issue. I solved this by this way
1) You're not using UIViewController's designated initializer initWithNibName:bundle:. Try using it instead of just init.
2) set animated:YES to a NO, and that solved the problem.
eg. [self.navigationController pushViewController: viewController_Obj animated:NO];
I had the same issue using navigation controller and push other controllers to it.
I tried to use Buffered Navigation Controller and several other approaches, but it didn't work for me. After spending some time for figuring it out I noticed that this issue occurs if you trying to push new view controller while previous transaction (animation) in progress (about 0.5 sec duration I guess). Anyway, I made quick solution with delegating navigation controller and waiting for previous animation finishes.
Ensure that you do not forget to in -viewWillAppear, -viewDidAppear, -viewDidLoad, -viewWillDisappear, -viewDidDisappear to call proper super method in your overload of that methods.
For example in my case I mismatched method name like this:
-(void)viewDidAppear
{
[super viewDidDisappear];
//some code staff
..
}
notice that appear and disappear methods are mismatched
'Unbalanced calls to begin/end appearance transitions for '
Says an animation is started before the last related animation isn't done. So, are you popping any view controller before pushing the new one ? Or may be popping to root ? if yes try doing so without animation i.e. [self.navigationController popToRootViewControllerAnimated:NO];
And see if this resolves the issue, In my case this did the trick.
I had a similar problem that involved rewinding modal dialogs. Posted the solution here...
https://stackoverflow.com/a/38795258/324479
[Problem]
Nav Controller -> VC1 -Push--> VC2 -PopOver or Modal Segue--> VC3.
VC3 is unwinding back to VC1.
When the Segue from VC2 to VC3 is PopOver and Modal, the unwind ends in a warning: Unbalanced calls to begin/end appearance transitions for UIViewController"
If the Segue from VC to VC is push, the warning is gone.
[Solution]
It would be great if the unwind logic would take care of this. Maybe it's a bug, maybe not. Either way, the solution is to make VC2 (The controller that has the popup) the target of the rewind, then wait for it to finish appearing before popping up the nav controller. That ensures the rewind (reverse popup) animation has enough time to finish before moving further back. Even with the animations off, it still has to wait or else you get the error.
Your code for VC2 should be as follows. (Swift)
class VC2: UIViewController {
private var unwind = false
#IBAction func unwindToVC1(segue:UIStoryboardSegue) {
unwind = true
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if unwind {
self.navigationController?.popViewControllerAnimated(false)
}
}
}
I got this issue because I was calling a UIPrintInteractionController from a viewController without UITabbar, and neither UINavigationBar. It seems that the UIPrintInteractionController didn't get the correct printInteractionControllerParentViewController.
Implementing the method in the delegate and return the current rootViewController worked for me.
- (UIViewController*)printInteractionControllerParentViewController:(UIPrintInteractionController*)printInteractionController;
Swift 4
My issue was that I was presenting another VC before my current one finished to be rendered .
Solution was to present my nextVC after a quick delay.
WHAT YOU SHOULD NOT DO
override func viewDidLoad() {
super.viewDidLoad()
self.present(MyNextVC(), animated: true, completion: nil)
}
WHAT YOU SHOULD DO
override func viewDidLoad() {
super.viewDidLoad()
//Wait until the view finished to be constructed
perform(#selector(showMyNextVC), with: nil, afterDelay: 0.01)
}
#objc func showCityList() {
self.present(MyNextVC(), animated: true, completion: nil)
}
The situation can occur if you are adding a view with a modal view controller as a sub view. Best to use:
-(void) viewDidAppear:(BOOL)animated {
[self presentViewController:self.yourModalVC animated:YES completion:nil];
}
It is basically saying the view life cycle is not streamlined for those viewControllers you are trying to display then.
I have the similar problem when trying to do:
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
in a function like - (void) popUpToLevelTwo; , and to put a return; at the end of function solves the problem
I also got this at
[self dismissModalViewControllerAnimated:YES];
I changed the YES to a NO, and that solved the problem.
i have same problem when i used navigationcontroller's pop method
In my app i use a separate logic for navigation controller,So avoided the use of navigation bar and it is always hidden. Then i use a custom view and notification for handling the backbutton and it's events. notification observers are registered and and not removed. So the notification fires twice, and it creates the above mentioned error. Check your code throughly for getting such fault's
For what it's worth, I got this same error when not including a call to [super viewDidLoad:animated] in my viewDidLoad override.
I also had this problem when I tapped a button from a NIB. It turns out I had accidentally wired the button to send an event to two IBAction methods, each of which did a pushViewController:animated:
I had some logic implemented to wait pushing the UIViewController until all data was downloaded. There was an error in this logic which caused to push the UIViewController too early while there was still another API call in progress.
It caused the same UIViewController to be pushed twice by the UINavigationController and gave this warning.
Reason For message: This message get displayed if and only if you are pushing/presenting another View controller from viewWillAppear,loadView,init or viewDidLoad method of current View Controller
Way to Remove error Message: Move your pushing/presenting code to viewDidAppear method will solve the issue
I had this problem when I forget to set Break; after pushing the view in a switch statement!
Like here:
case 1:{
SomeViewController *someViewController = [[SomeViewController alloc]initWithNibName:#"SomeViewController" bundle:Nil];
[self.navigationController pushViewController:someViewController animated:YES];
[someViewController release];
}
break; //Forgetting to set break here:
the reason behind the error " Unbalanced calls to begin/end appearance transitions" is when you navigate | segue twice at the same time
one solution would be,
[NSTimer scheduledTimerWithTimeInterval:0.05(or as required) target:self
selector:#selector(your_selector_method_to_push_the_view) userInfo:nil repeats:NO];
You can run into this if you try to dismiss a UIViewController before it is finished loading.
I had this message in the console and was focusing entirely on the UIViewController that was presenting the new UIViewController, without success. I finally discovered the problem was in the UIViewController I was presenting was dismissing itself because the user wasn't logged into their account.
Hope this helps someone.
This was a tough one for me:
I've overridden
override func shouldAutomaticallyForwardRotationMethods() -> Bool {
return true
}
without overriding:
override func shouldAutomaticallyForwardAppearanceMethods() -> Bool {
return true
}
in my window root navigation controller.
then a child navigation controller complained when pushing another view controller with the above mentioned warning.
The warning wasn't the worst,
the big problem was, that there the delegate of the child navigation controller weren't called anymore.
weired.
In my case I was fetching NSData from NSURL inside 'viewDidLoad' method.

Invocation of ViewDidLoad after initWithNibName

It appears that ViewDidLoad() is sent to a ViewController only after its View is physically displayed (i.e. via NavigationController pushViewController), and not immediately after initWithNibName(). Is this a behavior I can rely on? I would like to get the chance to set the member variables of my view so that all the members are valid by the time ViewDidLoad() is invoked.
You can set up member variables and other such things in initWithNibName:bundle:.
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if (self = [super initWithNibName:nibName bundle:nibBundle]) {
// set up ivars and other stuff here.
someIvar = someValue;
}
return self;
}
You are correct that viewDidLoad: is only sent when the view is physically displayed, i.e when it is added to some visible view (which may sometimes be never if the user does not reach that view). So it's useful to split the functionality and think about what you can do at init time and what happens at view load time.
As Marcelo Cantos notes in the comment, viewDidLoad: is generally a fine place to do all sorts of setup work, using the concept of "lazy loading," so that you defer the setup until as late time as possible.
viewDidLoad is called before a view controller is displayed for the first time, not immediately after initWithNibName. For example, if you have a tab bar controller, all of the child view controllers will be initd at launch, but viewDidLoad will only be called when you click on the appropriate tab the first time. It's generally a good idea to initialize memory-intensive items in viewDidLoad, so as to avoid using unnecessary memory.
I found that if I override initiWithNibName in the view controller, the viewDidLoad method is not called. I have to call it manually [self viewDidLoad]. But if I do not override initWithNibName: viewDidLoad is called. I am working with 4 view controllers in tab bar controller. the tab bar controller is loaded from another view.
Sorry to unearth an old thread, but this solved it for me...
-(void)viewDidLoad is only called after -(void)loadView has done its thing. In the docs for loadView:
The view controller calls this method when its view property is requested but is currently nil.
My view controller only has viewDidLoad called after its view is request by a UITabBarItem, meaning viewDidLoad is only called in the viewController once the tab bar button is pressed. I, like the OP, want viewDidLoad to be called directly after the nib is loaded, so it's contents (titles, etc) can be populated before the user clicks the tab button.
So, after calling "self = [super initWithNibName:#"nibName" bundle:nil];" in the view controller's custom initialiser, I immediately called '[self view]' afterwards. As the view is requested earlier than when it is requested by the UITabBarItem (which calls 'addSubview'), the view is initialised fully during initialisation, rather than when requested.
Hope this helps.

What happens to the root view in UINavigationViewController when you push on a new view?

Specifically, what am I supposed to do with the view that is now hidden after pushing on a new view controller?
In my situation I have a view with animations going on, that continue to execute after the view is off screen.
Is there some accepted convention?
Do I remove the View Controller and View from memory?
Does Cocoa Touch have a convenient method to "Pause" a view (and controller) and remove it from memory, and bring it back into existence when needed (after a pop)?
Do I have to archive it myself and then un-archive it?
Are there any examples you can point me to?
Thanks
Another possible solution is to implement two of the following methods:
– viewWillAppear:
– viewDidAppear:
– viewWillDisappear:
– viewDidDisappear:
You could potentially stop your animation in viewWillDisappear or viewDidDisappear and then restart it in viewWillAppear or viewDidAppear. You could also store any necessary state information about the animation before you stop it.
Under low memory conditions, your controller's 'view' property will automatically be set to nil, if it's not on screen. Then the view will automatically load again when it is needed - and viewDidLoad should get called at that time.
If your view controller is retaining any subviews of the top-level view, then you may want to override the view controller's setView: method, check if the view is being set to nil, and if so, release subviews that you were retaining. Otherwise, the top-level view may never get deallocated.
- (void)setView:(UIView *)view
{
[super setView:view];
if (view == nil)
{
// Release other views
// self.someView = nil;
}
}
Whenever you call pushViewController, the current viewcontroller is stored in an array by navigation controller (this can be accessed using the viewControllers property of navcontroller).
Think of it as a stack. As you call pushViewController, a new viewcontroller is added to the stack on top of the current viewcontroller. But your rootviewcontroller is still in memory. When you call popViewController, that viewcontroller is removed from the stack and released.
So if you want to stop your animation when the view disappears, use the viewWillDisappear method as suggested by Andy.

iPhone viewWillAppear not firing

I've read numerous posts about people having problems with viewWillAppear when you do not create your view hierarchy just right. My problem is I can't figure out what that means.
If I create a RootViewController and call addSubView on that controller, I would expect the added view(s) to be wired up for viewWillAppear events.
Does anyone have an example of a complex programmatic view hierarchy that successfully receives viewWillAppear events at every level?
Apple's Docs state:
Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed.
The problem is that they don't describe how to do this. What does "directly" mean? How do you "indirectly" add a view?
I am fairly new to Cocoa and iPhone so it would be nice if there were useful examples from Apple besides the basic Hello World crap.
If you use a navigation controller and set its delegate, then the view{Will,Did}{Appear,Disappear} methods are not invoked.
You need to use the navigation controller delegate methods instead:
navigationController:willShowViewController:animated:
navigationController:didShowViewController:animated:
I've run into this same problem. Just send a viewWillAppear message to your view controller before you add it as a subview. (There is one BOOL parameter which tells the view controller if it's being animated to appear or not.)
[myViewController viewWillAppear:NO];
Look at RootViewController.m in the Metronome example.
(I actually found Apple's example projects great. There's a LOT more than HelloWorld ;)
I finally found a solution for this THAT WORKS!
UINavigationControllerDelegate
I think the gist of it is to set your nav control's delegate to the viewcontroller it is in, and implement UINavigationControllerDelegate and it's two methods. Brilliant! I'm so excited i finally found a solution!
Thanks iOS 13.
ViewWillDisappear, ViewDidDisappear, ViewWillAppear and
ViewDidAppear won't get called on a presenting view controller on
iOS 13 which uses a new modal presentation that doesn't cover the
whole screen.
Credits are going to Arek Holko. He really saved my day.
I just had the same issue. In my application I have 2 navigation controllers and pushing the same view controller in each of them worked in one case and not in the other. I mean that when pushing the exact same view controller in the first UINavigationController, viewWillAppear was called but not when pushed in the second navigation controller.
Then I came across this post UINavigationController should call viewWillAppear/viewWillDisappear methods
And realized that my second navigation controller did redefine viewWillAppear. Screening the code showed that I was not calling
[super viewWillAppear:animated];
I added it and it worked !
The documentation says:
If you override this method, you must call super at some point in your implementation.
I've been using a navigation controller. When I want to either descend to another level of data or show my custom view I use the following:
[self.navigationController pushViewController:<view> animated:<BOOL>];
When I do this, I do get the viewWillAppear function to fire. I suppose this qualifies as "indirect" because I'm not calling the actual addSubView method myself. I don't know if this is 100% applicable to your application since I can't tell if you're using a navigation controller, but maybe it will provide a clue.
Firstly, the tab bar should be at the root level, ie, added to the window, as stated in the Apple documentation. This is key for correct behavior.
Secondly, you can use UITabBarDelegate / UINavigationBarDelegate to forward the notifications on manually, but I found that to get the whole hierarchy of view calls to work correctly, all I had to do was manually call
[tabBarController viewWillAppear:NO];
[tabBarController viewDidAppear:NO];
and
[navBarController viewWillAppear:NO];
[navBarController viewDidAppear:NO];
.. just ONCE before setting up the view controllers on the respective controller (right after allocation). From then on, it correctly called these methods on its child view controllers.
My hierarchy is like this:
window
UITabBarController (subclass of)
UIViewController (subclass of) // <-- manually calls [navController viewWill/DidAppear
UINavigationController (subclass of)
UIViewController (subclass of) // <-- still receives viewWill/Did..etc all the way down from a tab switch at the top of the chain without needing to use ANY delegate methods
Just calling the mentioned methods on the tab/nav controller the first time ensured that ALL the events were forwarded correctly. It stopped me needing to call them manually from the UINavigationBarDelegate / UITabBarControllerDelegate methods.
Sidenote:
Curiously, when it didn't work, the private method
- (void)transitionFromViewController:(UIViewController*)aFromViewController toViewController:(UIViewController*)aToViewController
.. which you can see from the callstack on a working implementation, usually calls the viewWill/Did.. methods but didn't until I performed the above (even though it was called).
I think it is VERY important that the UITabBarController is at window level though and the documents seem to back this up.
Hope that was clear(ish), happy to answer further questions.
As no answer is accepted and people (like I did) land here I give my variation. Though I am not sure that was the original problem. When the navigation controller is added as a subview to a another view you must call the viewWillAppear/Dissappear etc. methods yourself like this:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[subNavCntlr viewWillAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[subNavCntlr viewWillDisappear:animated];
}
Just to make the example complete. This code appears in my ViewController where I created and added the the navigation controller into a view that I placed on the view.
- (void)viewDidLoad {
// This is the root View Controller
rootTable *rootTableController = [[rootTable alloc]
initWithStyle:UITableViewStyleGrouped];
subNavCntlr = [[UINavigationController alloc]
initWithRootViewController:rootTableController];
[rootTableController release];
subNavCntlr.view.frame = subNavContainer.bounds;
[subNavContainer addSubview:subNavCntlr.view];
[super viewDidLoad];
}
the .h looks like this
#interface navTestViewController : UIViewController <UINavigationControllerDelegate> {
IBOutlet UIView *subNavContainer;
UINavigationController *subNavCntlr;
}
#end
In the nib file I have the view and below this view I have a label a image and the container (another view) where i put the controller in. Here is how it looks. I had to scramble some things as this was work for a client.
Views are added "directly" by calling [view addSubview:subview].
Views are added "indirectly" by methods such as tab bars or nav bars that swap subviews.
Any time you call [view addSubview:subviewController.view], you should then call [subviewController viewWillAppear:NO] (or YES as your case may be).
I had this problem when I implemented my own custom root-view management system for a subscreen in a game. Manually adding the call to viewWillAppear cured my problem.
Correct way to do this is using UIViewController containment api.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIViewController *viewController = ...;
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
I use this code for push and pop view controllers:
push:
[self.navigationController pushViewController:detaiViewController animated:YES];
[detailNewsViewController viewWillAppear:YES];
pop:
[[self.navigationController popViewControllerAnimated:YES] viewWillAppear:YES];
.. and it works fine for me.
A very common mistake is as follows.
You have one view, UIView* a, and another one, UIView* b.
You add b to a as a subview.
If you try to call viewWillAppear in b, it will never be fired, because it is a subview of a
iOS 13 bit my app in the butt here. If you've noticed behavior change as of iOS 13 just set the following before you push it:
yourVC.modalPresentationStyle = UIModalPresentationFullScreen;
You may also need to set it in your .storyboard in the Attributes inspector (set Presentation to Full Screen).
This will make your app behave as it did in prior versions of iOS.
I'm not 100% sure on this, but I think that adding a view to the view hierarchy directly means calling -addSubview: on the view controller's view (e.g., [viewController.view addSubview:anotherViewController.view]) instead of pushing a new view controller onto the navigation controller's stack.
I think that adding a subview doesn't necessarily mean that the view will appear, so there is not an automatic call to the class's method that it will
I think what they mean "directly" is by hooking things up just the same way as the xcode "Navigation Application" template does, which sets the UINavigationController as the sole subview of the application's UIWindow.
Using that template is the only way I've been able to get the Will/Did/Appear/Disappear methods called on the object ViewControllers upon push/pops of those controllers in the UINavigationController. None of the other solutions in the answers here worked for me, including implementing them in the RootController and passing them through to the (child) NavigationController. Those functions (will/did/appear/disappear) were only called in my RootController upon showing/hiding the top-level VCs, my "login" and navigationVCs, not the sub-VCs in the navigation controller, so I had no opportunity to "pass them through" to the Nav VC.
I ended up using the UINavigationController's delegate functionality to look for the particular transitions that required follow-up functionality in my app, and that works, but it requires a bit more work in order to get both the disappear and appear functionality "simulated".
Also it's a matter of principle to get it to work after banging my head against this problem for hours today. Any working code snippets using a custom RootController and a child navigation VC would be much appreciated.
In case this helps anyone. I had a similar problem where my ViewWillAppear is not firing on a UITableViewController. After a lot of playing around, I realized that the problem was that the UINavigationController that is controlling my UITableView is not on the root view. Once I fix that, it is now working like a champ.
I just had this problem myself and it took me 3 full hours (2 of which googling) to fix it.
What turned out to help was to simply delete the app from the device/simulator, clean and then run again.
Hope that helps
[self.navigationController setDelegate:self];
Set the delegate to the root view controller.
In my case problem was with custom transition animation.
When set modalPresentationStyle = .custom viewWillAppear not called
in custom transition animation class need call methods:
beginAppearanceTransition and endAppearanceTransition
For Swift. First create the protocol to call what you wanted to call in viewWillAppear
protocol MyViewWillAppearProtocol{func myViewWillAppear()}
Second, create the class
class ForceUpdateOnViewAppear: NSObject, UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool){
if let updatedCntllr: MyViewWillAppearProtocol = viewController as? MyViewWillAppearProtocol{
updatedCntllr.myViewWillAppear()
}
}
}
Third, make the instance of ForceUpdateOnViewAppear to be the member of the appropriate class that have the access to the Navigation Controller and exists as long as Navigation controller exists. It may be for example the root view controller of the navigation controller or the class that creates or present it. Then assign the instance of ForceUpdateOnViewAppear to the Navigation Controller delegate property as early as possible.
In my case that was just a weird bug on the ios 12.1 emulator. Disappeared after launching on real device.
I have created a class that solves this problem.
Just set it as a delegate of your navigation controller, and implement simple one or two methods in your view controller - that will get called when the view is about to be shown or has been shown via NavigationController
Here's the GIST showing the code
ViewWillAppear is an override method of UIViewController class so adding a subView will not call viewWillAppear, but when you present, push , pop, show , setFront Or popToRootViewController from a viewController then viewWillAppear for presented viewController will get called.
My issue was that viewWillAppear was not called when unwinding from a segue. The answer was to put a call to viewWillAppear(true) in the unwind segue in the View Controller that you segueing back to
#IBAction func unwind(for unwindSegue: UIStoryboardSegue, ViewController subsequentVC: Any) {
viewWillAppear(true)
}
I'm not sure this is the same problem that I solved.
In some occasions, method doesn't executed with normal way such as "[self methodOne]".
Try
- (void)viewWillAppear:(BOOL)animated
{
[self performSelector:#selector(methodOne)
withObject:nil afterDelay:0];
}
You should only have 1 UIViewController active at any time. Any subviews you want to manipulate should be exactly that - subVIEWS - i.e. UIView.
I use a simlple technique for managing my view hierarchy and have yet to run into a problem since I started doing things this way. There are 2 key points:
a single UIViewController should be used to manage "a screen's worth"
of your app
use UINavigationController for changing views
What do I mean by "a screen's worth"? It's a bit vague on purpose, but generally it's a feature or section of your app. If you've got a few screens with the same background image but different overlays/popups etc., that should be 1 view controller and several child views. You should never find yourself working with 2 view controllers. Note you can still instantiate a UIView in one view controller and add it as a subview of another view controller if you want certain areas of the screen to be shown in multiple view controllers.
As for UINavigationController - this is your best friend! Turn off the navigation bar and specify NO for animated, and you have an excellent way of switching screens on demand. You can push and pop view controllers if they're in a hierarchy, or you can prepare an array of view controllers (including an array containing a single VC) and set it to be the view stack using setViewControllers. This gives you total freedom to change VC's, while gaining all the advantages of working within Apple's expected model and getting all events etc. fired properly.
Here's what I do every time when I start an app:
start from a window-based app
add a UINavigationController as the window's rootViewController
add whatever I want my first UIViewController to be as the rootViewController of the nav
controller
(note starting from window-based is just a personal preference - I like to construct things myself so I know exactly how they are built. It should work fine with view-based template)
All events fire correctly and basically life is good. You can then spend all your time writing the important bits of your app and not messing about trying to manually hack view hierarchies into shape.