Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
James Andrews
7,245 PointsSending data from one view controller to the previous view controller?
After running through all of the projects I have a pretty good grasp on things, but I am forgetting how to do something and not sure which video to look back into, so I am hoping some one can point me in the right direction. I started writing my first original application and am stuck on something.
I have a view that has a button. When that button is selected I push a table view modally to the screen, when an item is selected on that table view I want to return back to the original view, but also carry that selection with me back to the view. Any help would be appreciated .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *selected = [self.dataManager.categories objectAtIndex:indexPath.row];
NSLog(@"%@", selected);
[self.navigationController popToRootViewControllerAnimated:YES];
// Here is where I don't know how to send "selected" back to the previous view controller.
}
2 Answers
Robert Bojor
Courses Plus Student 29,439 PointsHi James,
I've created a project to show you how the delegation works between two View Controllers. You can grab it here: https://dl.dropboxusercontent.com/u/7220030/DelegateTest.zip
Basically, you need to declare a @protocol and inside it a method, in the first VC, that will handle the received data and also register your First VC to use this delegate that was just created.
// RBFirstVC.h
#import <UIKit/UIKit.h>
@protocol RBFirstVCDelegate <NSObject>
- (void)receiveMessage:(NSString *)message;
@end
@interface RBFirstVC : UIViewController <RBFirstVCDelegate>
@end
The implementation file looks like this:
// RBFirstVC.m
#import "RBFirstVC.h"
#import "RBSecondVC.h"
@interface RBFirstVC ()
// This is the label that will receive the text sent from the second view controller
@property (weak, nonatomic) IBOutlet UILabel *responseLabel;
@end
@implementation RBFirstVC
// Before we present the Second View Controller we have to declare it's delegate to be the First View Controller
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
RBSecondVC *secondVC = [segue destinationViewController];
RBFirstVC *firstVC = [segue sourceViewController];
secondVC.delegate = firstVC;
}
// This will just update the label text with the message we get from the Second View Controller
- (void)receiveMessage:(NSString *)message {
_responseLabel.text = message;
}
// This is the button that triggers the modal presentation
- (IBAction)openSecondAction:(id)sender {
[self performSegueWithIdentifier:@"openSVC" sender:self];
}
@end
The Second View Controller's header file will look like this:
// RBSecondVC.h
#import <UIKit/UIKit.h>
#import "RBFirstVC.h"
@class RBFirstVC;
@interface RBSecondVC : UIViewController
@property (weak, nonatomic) id<RBFirstVCDelegate> delegate;
@end
And the implementation...
// RBSecondVC.m
#import "RBSecondVC.h"
@interface RBSecondVC ()
// This is the text field that will contain the message
@property (weak, nonatomic) IBOutlet UITextField *messageForFVC;
@end
@implementation RBSecondVC
// This is the button that closes the modal but first sends the message to its delegate
- (IBAction)closeAndSend:(id)sender {
[self.delegate receiveMessage:_messageForFVC.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
You will see it better in the project files since I've used the Storyboard to link the two View Controllers to each other through a segue.
Have fun, hope this helps.
Robert Bojor
Courses Plus Student 29,439 PointsHi James,
One easy option would be to use NSUserDefaults. Store whatever you want to store just before the popToRootViewControllerAnimated: and remember to synchronize.
The second option would be to use a delegate that will send everything back to the previous ViewController.
James Andrews
7,245 Pointsso how do I set one view controller as the delegate for the other if both view controllers are in a storyboard? I've been unable to figure that out...
James Andrews
7,245 PointsJames Andrews
7,245 PointsThanks. I'll give it a try later this week and let ya know how it goes.
James Andrews
7,245 PointsJames Andrews
7,245 Pointsi decided that 3am was the perfect time to try it. I have it working. Now need to solve other issues. thanks for the help.