viewWillDisappear not called when calling popToRootViewControllerAnimated - ios

I work on a legacy application, and have found out, that my view[Will/Did]Disappear methods are not always fired properly.
The case is, I have a (custom) UIViewController set as rootViewController in AppDelegate. This rootViewController has a UINavigationController, which has two view controllers pushed on it. When the user presses the home button, the user is logged out. When he later returns to the app, the application calls [UINavigationController popToRootViewControllerAnimated:YES] and then displays a modal UIViewController for logging in.
The problem is: When I push/pop on the UINavigationController normally, my viewWillDisappear method is called properly. But when I use the popToRootViewControllerAnimated: method, viewWillDisappear is not called on any of the viewControllers that are popped off.
Searching on the internet has only given two possible reasons:
If using a UINavigationController as a subview, you must call view[Will/Did]Disappear yourself
Not calling the proper super methods
None of these suggestions are the case in my app. And I have no idea where to look. Anybody has a suggestion to what has been done wrong in the app?

The view probably wasn't onscreen. It has to be onscreen (visible) for the viewWillDisappear: method to be called. If it's coming back from the background, it wasn't visible.
You could try using willMoveToParentViewController: which is called when the view controller is removed from its parent.

such useful to me
[nav performSelector:#selector(popToRootViewControllerAnimated:) withObject:nil afterDelay:0.0];
I rewrote UITabBarController
- (void)setSelectedIndex:(NSUInteger)selectedIndex {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UINavigationController *navigationController = [originalViewController as:[UINavigationController class]];
if (navigationController.presentedViewController) {
[navigationController dismissViewControllerAnimated:NO completion:^{
[navigationController popToRootViewControllerAnimated:NO];
}];
}else if (navigationController.topViewController){
[navigationController popToRootViewControllerAnimated:NO];
}
});
}

Related

viewWillAppear not getting called

I have a view controller(VC1) embedded in a navigation controller(NAV1). In its viewWillAppear method, I make a call to modally present another view controller. In one case I need the new view controller(VC2) to be presented with animation, and in another case it should be presented without animation. VC2 is also embedded in its own navigation controller(NAV2).
All is fine when the animation flag is set to TRUE. When I set the flag to FALSE, couple of things go wrong:
1. I get the following warning in the console: Presenting view controllers on detached view controllers is discouraged
2. When I move back from VC2 after calling dismissViewControllerAnimated:FALSE completion:nil the viewWillAppear method of VC1 does not get called. It gets called if the animation flag is set to TRUE.
In VC1:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self performSelector:#selector(importStuff:)
withObject:nil];
}
-(void)importStuff
{
//instatntiate VC2
//instantiate NAV2
[Utility presentViewController:pNavController
fromViewController:self
animated:FALSE
completion:nil];
// the above method calls the presentViewController:animated:completion: method
}
IN VC2:
[Utility dismissViewController:self
animated:FALSE
completion:nil];
The above method calls the dismissViewControllerAnimated: method.
Not a solution but a workaround:
You can easily postpone any UI operation to the very next event loop by using afterDelay:0:
[self performSelector:#selector(importStuff:)
withObject:nil
afterDelay:0];
This will give a chance to the current operation to complete.
Furthermore, a delay of 0.4 will match the OS. However, whatever delay you use (other than 0) is a kludge and not guaranteed to work under every situation, device and memory load, etc.
Instead, you should revisit your approach.
Change your design:
Do not run the risk of encountering this animation race in the first place.
You have a couple of options, including:
Controlling the transition animation yourself and waiting for its completion prior pushing another view controller (using a completion signal or completion block)
Changing your methodology entirely to avoid this conundrum altogether
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self performSelector:#selector(importStuff) withObject:nil afterDelay:0.1];
}
-(void)importStuff
{
//instatntiate VC2
//instantiate NAV2
SecondViewController *viewMe=[self.storyboard instantiateViewControllerWithIdentifier:#"second"];
[self.view.window.rootViewController presentViewController:viewMe animated:NO completion:nil];
// the above method calls the presentViewController:animated:completion: method
}

Pop THE RootViewController

I am developing an app that as first viewcontroller has an UIViewController.
This controller pushes a NavigationViewController that contains other controllers.
Now I need to pop the RootViewController of the NavigationController to go back to the initial UIViewController.
I tried with
[self dismissViewControllerAnimated:YES completion:nil];
and the app crashes("Tread1:EXC_BAD_ACCESS(code=1, address= ......)").
I tried with
[self.navigationController popViewControllerAnimated:YES];
and nothing happens.
The initial UIViewController calls
[self performSegueWithIdentifier:#"MyIdentifier" sender:self];
In the UIBuilder the segue is of type "Show(e.g. Push)"
Then I have a NavigationViewcontroller that contains the RootViewController and another Viewcontroller.
What I am trying to achieve is to go back to the first viewcontroller (the one outside the navigationcontroller) from the RootViewController. So I should have the navigationcontroller there.
What am I missing?
Apparently I had a couple of GestureRecognizer still in place that were causing the crash of the app.
The right method was:
[self dismissViewControllerAnimated:YES completion:nil];

pushViewController in viewWillAppear failed after popToRootViewController

there have 3 view controllers, A,B,C, all designed in storyboard.
in all pushViewController: controllers were got from [self.navigationController instantiateViewControllerWithIdentifier:#"A/B/C_Controller"];
In A_ViewController ( root controller ):
-(void)viewWillAppear
{
[self.navigationController pushViewController:B_Controller animated:YES];
}
In B_ViewController:
-(IBAction)someButtonClick
{
[self.navigationController pushViewController:C_Controller animated:YES];
}
In C_ViewController:
-(IBAction)someButtonClick
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Problem:
A_viewController -> B_viewController -> C_viewController is fine.
but after popToRootViewController to A From C, A can't push to B anymore.
and other pushViewController: all can't works, popViewControllerAnimated: also cause crash.
Note.
have tried
// in A view
dispatch_async(dispatch_get_main_queue(),^{
[self.navigationController pushViewController:B_viewController animated:YES];
})
in this post, but it doesn't works for me.
does anyone know what happens in view controller stacks?
have struggling in this issue all day, any help will be appreciated!
When you When you popToViewController from C, the viewWillAppear method will in A be called just before the animation. Now, at this point, since the transition is in progress, you might get some kind of warning in the debugger like:
"Warning: Attempting to push .... while a transition is in progress"
You need to call the navigation controller methods in such a way that they do not overlap each other.
Calling pushViewController in the viewDidAppear method of A would be better in this context.

Navigation controller popViewControllerAnimated : yes is not working as expected

I am using following line of code:
[self.navigationController popViewControllerAnimated:YES];
But it is not behaving in ios 7 as it doing in ios 6.Some times it does not pop controller while we are pressing back button 2- 3 times in succession.
Resulting in abrupt behaviour in navigation bar and deallocating a controller but showing the same on ui .
So when we press anything on that controller it results to a crash since controller is already deallocated.
Check if you're running the code on the UI thread
[self.navigationController popToRootViewControllerAnimated:YES];
This method will navigate to the root of your navigationController.
You can check your viewController hierachy With following code.
NSLog(#"%#",self.navigationController.viewControllers);
I resolved this problem with this way:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UINavigationController * nav = tabbarControl.selectedViewController;
[nav.viewControllers objectAtIndex:0];
[nav setViewControllers:#[[nav.viewControllers objectAtIndex:0]] animated:NO];
tabbarControl.selectedIndex = 0;
});
When you delay one second the view will pop from UI, then the view will pop from the navigation stack, I think is the problem of the animation serial.
I had the same problem on iOS 8.
I solved by subclassing UINavigationController and adding this code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
return [super popViewControllerAnimated:animated];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
{
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
I basically block all the user interactions during the pop animation. I know it's a dirty solution, but it's the only one that I found that solves the problem.
I think that should be working without dispatch_async.
I got to the same issue, but i got to know the reason.
We should check it if the current scene is assigned to a proper view controller name in the storyboard.(identity inspector -> class)
If you connect a button action to m file and then insert the name of the view controller, that is not working.
So, you should delete the connect, and you insert the proper view controller name, and then you should connect the action to m file again.
I created my project from master-detail template, that uses split view controller. In my case, removing the split view controller resolved this issue.
It is important that that calls to popViewController(animated:), popToRootViewController(animated:) and related calls be made in the main queue, but under some conditions this doesn't seem to be good enough, and the animation doesn't occur.
I was able to fix it as described in some other answers here, performing the pop navigation later in the main queue. The Swift equivalent:
DispatchQueue.main.async {
self.rootViewController.popViewController(animated: true)
}
This might be explained by other animations that are still in progress, and by scheduling the block this way it happens at the end of the current work taking place or currently scheduled in the main queue, which allows the animation to execute correctly.
Try this code for popup a view controller from navigation stack
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count -2] animated:YES];

iOS Delegate Does Not Push or Present a View

I have a HomeView and a HomeDropDownView.
HomeDropDownView is shown as a drop-down view over the HomeView.
HomeView is a delegate of HomeDropDownView.
When I do an action in HomeDropDownView I want to call a delegate method in HomeView and have that delegate method present a third view controller, TestViewController from it's navigation controller.
If I try to launch TestViewController from anywhere in the class it works fine - except from the delegate method.
There are animations in HomeDropDownView but putting the call to the delegate method in the complition does not make the view controller appear. And in the case that I'm using this the animation's don't fire anyway; there's only a resizing without animation.
TestViewController's init does get called as well as the viewDidLoad but not the viewWillAppear and the view dose not appear.
Code:
HomeDropDownView
- (void)finalAction {
...
[self callDelegateAction];
...
- (void)calldelegateAction {
if ([self.delegate respondsToSelector:#selector(launchTestView)] ) {
[self.delegate launchTestView];
} else {
DLog(#"Error out to the user.");
}
}
HomeView
- (void)launchTestView {
//[self listSubviewsOfView:self.parentViewController.view];
NSLog(#"delegate method | self: %#", self);
TestViewController *tvc = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
//[self.navigationController presentViewController:tvc animated:YES completion:nil];
//[self.view.window.rootViewController presentViewController:tvc animated:YES completion:nil];
//[self.navigationController pushViewController:tvc animated:YES];
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appdelegate.tabBarController.navigationController presentViewController:tvc animated:YES completion:^() {
NSLog(#"Done!");
}];
}
None of the above approaches work. But if I put the exact same code into the viewDidAppear or put it in a button action method, it will work fine. At the time of calling the delegate method's self is HomeView and all the subviews, including the nav controller do seem to be there. This is in a tabcontroller-based project but I think that any of the above are acceptable ways to call the nav controller still.
What am I missing? Why does my delegate method not want to push/present a viewcontroller on HomeView's Nav controller? It's probably something I'm missing but I can't find a reason in the Apple Docs or any other thread.
Thanks for the help!
Sadly this turned out to be that HomeView was being changed underneath the execution of the message. So by the time the HomeView got the message call it was no longer the same HomeView object that had requested action in the first place. So it was not the same delegate.
This was done so that it would appear to the user that the same view was being used for different things.
But this is a good example of why you should not destroy and re-create critical views. We should have been using the same view and reloading the objects instead if we knew that we would be sending messages. Or had some notion of a control structure.

Resources