This question already has an answer here:
-
Passing Data between View Controllers
42 answers
This is the scene of my storyboard.
UINavigationController
-> UITableViewController
-> push segue -> UIViewController
I know how to pass the data of UITableViewController
to UIViewController
.
But, how can I pass the data back to the UITableViewController
?(UIViewController
has a navigation bar with a back button)
You can do it through the use of delegation.
When you pass data of UITableViewController
to UIViewController
, you can set a delegate on UIViewController
. Whenever you want to pass data back to UITableViewController
, you can call the delegate and pass in the data
Here's a more concrete example in code
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
UIViewController *vc = [segue destinationViewController];
// Pass the UITableViewController to the UIViewController
[vc setDelegate:self];
}
}
Create a method on UITableViewController
that takes data from UIViewController
(example: -(void)processDataFromUIViewController:(NSString *)string
)
Now when you want to pass data back to UITableViewController
, call [delegate sendDataFromUIViewController:@"data"]