Search code examples
iosios7rubymotion

Triggering an event when user clicks "< Back" button


I have fairly simple UINavigationControllers with two screens: screen1 & screen2.

When the user is on screen2 and clicks < Back I want to trigger an event before screen1 is shown -- basically I want to fetch data from the server for screen1.

Is there a way to do this?


Solution

  • In your viewDidLoad do this:

    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
    self.navigationItem.leftBarButtonItem = backBarButton;
    

    Implement the method goBack:

    - (void)goBack:(id)sender {
        //Do something
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    Hope this helps.. :)