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
Kevin Duck
5,502 PointsWhy is this PFObject not being passed to a new view controller?
(Based on the Ribbit project)
I am struggling mightily trying to set "selectedJob" (which is a PFObject that has a job title and and name) in one view controller, then pass it to a new view controller, and then retrieve the job title from selectedJob. It appears that selectedJob is being set properly in the first view controller as I can NSLog the attributes of selectedJob in "didSelectRowAtIndexPath". However, when I try to access selectedJob from the NEW view controller, it returns (null) and crashes.
First view controller where selectedJob is set and works:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface jobsTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) PFObject *service;
@property (nonatomic, strong) PFObject *selectedJob;
@property (nonatomic, strong) NSArray *jobs;
@property (nonatomic, strong) NSData *messageData;
@property (nonatomic, strong) NSString *categoryName;
@property (nonatomic, strong) NSArray *ratings;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UIBarButtonItem *reportButton;
@end
#import "jobsTableViewController.h"
#import "serviceCategoriesViewController.h"
#import "serviceProvidersTableViewController.h"
#import <Parse/Parse.h>
@interface UITableViewController ()
@end
@implementation jobsTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
...
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedJob = [self.jobs objectAtIndex:indexPath.row];
NSLog(@"self.selectedJob (didselectrow): %@", self.selectedJob);
[self performSegueWithIdentifier:@"showServiceProviders" sender:self];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showServiceProviders"]) {
serviceProvidersTableViewController *serviceProviderViewController = [segue destinationViewController];
serviceProviderViewController.job = self.selectedJob;
NSLog(@"serviceProvidersViewController.job (prepare for segue): %@", [serviceProviderViewController.job objectForKey:@"jobTitle"]);
}
}
Second view controller where selectedJob keeps coming back null:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface serviceProvidersTableViewController : UITableViewController
@property (nonatomic, strong) PFObject *service;
@property (nonatomic, strong) PFObject *selectedJob;
@property (nonatomic, strong) PFObject *job;
@property (nonatomic, strong) PFObject *selectedServiceProvider;
@property (nonatomic, strong) NSArray *serviceProviders;
@property (nonatomic, strong) NSString *jobName;
@property (nonatomic, strong) NSArray *ratings;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UIBarButtonItem *reportButton;
@end
...
#import "jobsTableViewController.h"
#import "serviceCategoriesViewController.h"
#import "serviceProvidersTableViewController.h"
#import <Parse/Parse.h>
@interface UITableViewController ()
@end
@implementation jobsTableViewController
#import "serviceProvidersTableViewController.h"
#import "serviceCategoriesViewController.h"
#import "jobsTableViewController.h"
#import <Parse/Parse.h>
@interface UITableViewController ()
@end
@implementation serviceProvidersTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.jobName = [self.job objectForKey:@"jobTitle"];
NSLog(@"self.jobname (from providers class): %@", [self.job objectForKey:@"jobTitle"]);
self.title = [NSString stringWithFormat:@"%@", self.jobName];
self.navigationItem.title = self.title;
}
It appears that jobName comes back empty for some reason:
self.jobname (from providers class): (null)
2015-01-02 11:53:06.509 Ribbit[15029:222675] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot do a comparison query for type: (null)'
3 Answers
Sebastien Thomas
16,502 PointsHi Kevin,
In your prepareForSegue method, try casting your destination view controller :
serviceProvidersTableViewController *serviceProviderViewController = (serviceProvidersTableViewController*)[segue destinationViewController];
Kevin Duck
5,502 PointsUpdate: I believe I've solved this. It looked like prepareForSegue was being called twice, and at some point, the jobName was getting reset and null would get passed to the new view. I moved everything out of didSelectRowAtIndexPath and into prepareForSegue and it seems to have worked.
Sebastien Thomas
16,502 PointsWell spotted ! :)
Kevin Duck
5,502 PointsKevin Duck
5,502 PointsThanks, but no luck. The strange thing is that when I navigate to the new view controller, it seems to show one new view and then ANOTHER one automatically. The jobname comes back as null but THEN comes back properly during that second mysterious transition. I think I'm doing something wrong with the storyboard.