Domanda

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?

È stato utile?

Soluzione

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.. :)

Altri suggerimenti

If you're using storyboard, you can handle your back action in.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

Only by doing it in viewWillDisappear. Unfortunately it will be triggered also when you present a new screen over the current one, not just when you tap the back button. You could however add a custom back button, and place your code in it's action method. After the operation is finished, call [self.navigationController popViewControllerAnimated:YES]

 -(void)viewWillDisappear:(BOOL)animated{
    if (![self.navigationController.viewControllers containsObject:self]) {
        NSLog(@"Back Event");
    }
}

You can do in two way.

  1. Create custom button and set event.

  2. Check on viewWillDisappear when users pop the screen

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top