Reputation: 296
Currently, my app goes from view A to view B through a segue and performs some functions in prepareForSegue. What should I do if I want to call a function that is executed when view B goes to view A (after pressing the back button on the Navigation Controller)?
I imagine that I could map the "back button" to an IBAction, but I'm not sure how I would do that, since the back button is part of the Navigation Controller and isn't directly visible on the Storyboard.
In particular, view B updates some values that view A has displayed. I want to call viewDidLoad so that view A has the updated data before it loads.
In ViewA's view controller:
// update view when it becomes visible
- (void)viewDidAppear:(BOOL)animated {
[self viewDidLoad];
}
Upvotes: 0
Views: 1544
Reputation: 1083
In view controller "A" you need to implement the method prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
and inside create a segues destinationViewcontroller, heres a example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:SEGUE_FOR_OPTIONS])
{
// Get reference to the destination view controller
OptionsViewController *optionVC = [segue destinationViewController];
[optionVC someMethod];
}
}
Upvotes: 1
Reputation: 77661
Ah, this isn't a segue as such. This is equivalent to popViewController.
If you want to update the view when view A becomes visible then you can do that in the function...
- (void)viewWillAppear:(BOOL)animated
or in the function
- (void)viewDidAppear:(BOOL)animated
These will run each time the view becomes visible.
In here you can run your "updateView" method or whatever method you want.
If you want to pass data back to view A (i.e. view B is a "select city" view or something) then you show make view A a delegate of view B. That way you can pass the info back when the user does something.
Instead of running viewDidLoad do this...
- (void)viewDidLoad
{
[self.button setTitle:@"blah"];
[self updateView];
}
- (void)viewDidAppear:(BOOL)animated
{
[self updateView];
}
- (void)updateView
{
// do something that you want to happen
}
You shouldn't actually have to do this though. It is enough just to run [self updateView] from viewDidAppear.
You can't run viewDidLoad directly though.
Upvotes: 2