0

In my iOS app, the user progresses through the app like so:

VC1 > VC2 > VC3

I want to determine if the user presses the back button from VC3 to return to VC2 and execute some code if the user does so. I thought i would be able to do this within the didMoveToParentViewController method within VC2 and determining the previous view controller, but it isn't returning the right viewController (it's returning VC1).

What is the best way to do this?

This is what I currently have

-(void)didMoveToParentViewController:(UIViewController *)parent{
    NSLog(@"returned to VC2");
    NSInteger numberOfViewControllers = self.navigationController.viewControllers.count;

    if(numberOfViewControllers >=2){
        // returned to view -> reload projects
        NSLog(@"reloading projects from VC3");
        [self loadProjects:self.view];
    }
}
scientiffic
  • 9,045
  • 18
  • 76
  • 149

3 Answers3

1

If you are using a Storyboard then you can use an unwind segue.

In your VC2, add a method such as -

- (IBAction)unwindToVC2:(UIStoryboardSegue*)sender
{

    //Whatever you need to do
}

Then in the storyboard scene for VC3, drag a new Bar Button Item to the left of your Navigation Item. Select the button - change its title to "Back" or whatever you want it to say and then control drag from it to the green "Exit" icon at the bottom of the scene. Select "unwindToVC2:" and you are done.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • thanks for your help - this worked for me. but is there a way to style the bar button so it looks like the left back arrow? – scientiffic Aug 29 '14 at 02:44
  • Not easily unfortunately - http://stackoverflow.com/questions/3310454/creating-back-arrow-shaped-leftbarbuttonitem-on-uinavigationcontroller – Paulw11 Aug 29 '14 at 02:52
  • I see...but there must be some sort of callback when the user clicks back on the View Controller without setting up this new bar button! – scientiffic Aug 29 '14 at 02:55
  • `viewWillAppear` will be called, but you will need to store some property in order to determine if this is an initial presentation or a return from VC3 – Paulw11 Aug 29 '14 at 03:06
0

In the "AppDelegate.h" file, state that you implement the "UINavigationControllerDelegate" protocol:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

In the "AppDelegate.m" file, override this method of that protocol:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
   if ([viewController isKindOfClass:[SecondViewController class]])
   {
       // Insert the code here
   }
}

In the SecondViewController class, make a BOOL property that you will set to YES or NO, depending wether that method needed was executed or not.

ppalancica
  • 4,236
  • 4
  • 27
  • 42
  • Thanks for your help. I tried adding your suggestion to my AppDelegate files, but it seems like the methods are never called (I put in logs to check). Is it supposed to get called every time the user switches views? – scientiffic Aug 29 '14 at 02:25
  • You need to also set the navigationController's delegate property to self inside AppDelegate.m file. Did you already do that? – ppalancica Aug 29 '14 at 03:31
0

Thanks everyone for your advice. In the end, I took a different method. I created a boolean to determine whether a not a particular segue was initialized:

BOOL reloadProjects;

This boolean is set to YES when the segue is called. Then, when the user returns to the view, I reload the projects:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    if(reloadProjects == YES){
        [self loadProjects:self.view];
        reloadProjects = NO;
    }
}
scientiffic
  • 9,045
  • 18
  • 76
  • 149