I want to save DB when the back button clicked in navigation controller.
so I would insert code in method.
What method is called when back button clicked in navigation controller?
To do what you asked, look at the UINavigationControllerDelegate protocol, namely the method:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
when the viewController argument is no longer your view controller then you should save.
However, doing so on viewWillDisappear: might be a better (and much simpler) idea.
Maybe it's not appropriate use, but that worked for me. Don't forget to set UINavaigationController delegate.
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
NSLog(#"from VC class %#", [fromVC class]);
if ([fromVC isKindOfClass:[ControllerYouJustPopped class]])
{
NSLog(#"Returning from popped controller");
}
return nil;
}
Related
How to provide custom UIViewControllerAnimatedTransitioning and use default UIViewControllerInteractiveTransitioning for a navigation controller
#pragma mark UINavigationControllerDelegate
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC {
// Return custom transitioner
}
-(id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
return nil; // Use the deafult UIViewControllerInteractiveTransitioning here
}
There could be hacky ways, but not a clean way.
I need to pass an object to the root view controller of an UINavigationController before it appears. I have its root view controller relationship set in storyboard, and it seems that I can't handle it like the other types of segues.
How could I do this?
Thanks
You could assign the UINavigationController a delegate and implement the method -
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated {
// check viewController is kind of class, check any flags
// pass object to vc
}
I have a View Controller that has a custom push transition when a table cell is tapped and performs the standard pop transition when the back bar button item is tapped. The problem is when I try to go to the same view controller from the previous controller, the app crashes. Below is the UINavigationControllerDelegatefunction I'm implementing:
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
return (operation == UINavigationControllerOperationPush ? animator : nil);
}
Any clue is appreciated!
Thanks in advance.
I had a problem, where the NavigationController's delegate was set to a view controller that had been deallocated, which caused a crash in [UINavigationController _customTransitionController:].
Especially when using unwind segues, it does NOT seem that any intermediate view controllers receive a viewWillDisappear callback before getting deallocated. The remedy here is to implement the following in the destination unwind view controller:
-(IBAction)unwindSegue:(UIStoryboardSegue*)segue{
self.navigationController.delegate = nil;
}
I have an App where it displays a UIImagePickerController on launch. The controller displays the cancel button, but there is nothing to cancel to. Is there any way to remove the button?
NOTE: This is not a duplicate of Can the Cancel button be removed from a UIImagePickerController in OS 3.2?. The answer there for iOS 3.2 does not work for me in iOS 5.
I try to put UIImagePickerController not modally, but in original way (as we do it with ordinary custom UIViewController). So, I need no the cancel button in the UINavigationBar.
There are no documented ways to remove the cancel button. But here I found some idea.
I just added this code in my AppDelegate.m file:
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
for (UINavigationItem *item in navigationController.navigationBar.subviews) {
if ([[item title] compare:#"Cancel"] == 0) {
UIButton *button = (UIButton *)item;
[button setHidden:YES];
}
}
}
So the cancel button is hidden now. But if you pick some folder in you photo albums the next view will be with that button (and it is not hidden).
Then I tried to add code like this:
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
for (UINavigationItem *item in navigationController.navigationBar.subviews) {
if ([[item title] compare:#"Cancel"] == 0) {
UIButton *button = (UIButton *)item;
[button setHidden:YES];
}
}
}
I get the cancel button is hidden in all navigation stack inside UIImagePickerController's navigation.
But while the new view is appearing (animation transition) the cancel button is appearing too.
I don't know how to fix this problem.
However I think that it is a tricky and inefficient approach. Because it can break your application in the future iOS updates. So you can use the answer to that question. But it is different approach at all.
P.S. Sorry for my language.
For iOS 7 and 8 the above solution will not work,
There is small change that should be done because we cannot use navigation item from navigation bar. The following will work like a charm
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
viewController.navigationItem.rightBarButtonItems = nil;
}
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
viewController.navigationItem.rightBarButtonItems = nil;
}
I have AllowEditing set to true, and after the image is captured, I want to give additional instructions in the editing screen to crop out part of the image. Is that possible?
The UIImagePickerViewController is in fact a UINavigationController. This gives you the ability to track on which step in the UINavigationViewController you are, so you can set the overlay view only when the second view controller is reached.
In brief:
- UIImagePickerController.delegate = self
- make self conform to the UINavigationControllerDelegate protocol
- implement next method:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([navigationController.viewControllers indexOfObject:viewController] == 1)
{
// If second UIViewController, set your overlay.
}
}
Dires De Smet, your solution is good, but not good enough, cause you don't really know for sure what is your current view controller's index (of curse you can find it, but its ugly)
So i come with new if condition, check it out:
Assuming self is your current UIViewController and not some NSObject subclass,
if self it is NSObject subclass, so use:
UIViewController *currentViewController=(UIViewController *)[[[[[[[UIApplication sharedApplication]delegate]window]rootViewController]navigationController]viewControllers]lastObject];
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (![viewController isKindOfClass:[self class]]){
//do your thing...
}
}