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

iOS

Extra Credit: User Profiles > ProfileViewController> viewDidLoad

I'm trying to pull data from the selected user(from my table in FriendsViewController) via query and then set the data equal to my labels. But I'm struggling and I don't know how.

Here's my ProfileViewController.m :

  • (void)viewDidLoad { [super viewDidLoad];

        PFUser *selectedUser = [PFQuery getUserObjectWithId:self.userId];
        NSLog(@"username: %@" , selectedUser.username);
        self.userNameLabel.text = selectedUser.username; // Shows nothing
        self.emailLabel.text = selectedUser.email; //Shows nothing
    

}

Here's my Segue -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.identifier isEqualToString:@"showProfileView"]) {
    ProfileViewController *vc = (ProfileViewController *)segue.destinationViewController;
    vc.userId = self.selectedUserId; // could set this in didSelectRowAtIndexPath
}

}

My problem now is that the labels are returning null.

Amit Bijlani Stone Preston Ben Jakuben

4 Answers

oh ok thats why its null then. your segue is running before didSelectRowAtIndexPath runs. I assume you have a segue going from the tableView cell to your profile view controller.

delete that segue and instead create a manual segue from your tableViewController to your Profile View controller (select your tableViewCOntroller, then ctrl click and drag from the yellow circle at the bottom of the view controller to your profileViewController and release, then select manual segue, give it the same name as the old one) . using a manual segue will give you more control of when to fire it.

in your didSelectRowAtIndexPath method perform the segue AFTER you set the selectedUser id property

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    PFUser *user = [self.friends objectAtIndex:indexPath.row];
    self.selectedUserId = user.objectId;
     NSLog(@"Selected User ID : %@" , self.selectedUserId);

    //we set the property, now perform the segue
    [self performSegueWithIdentifier:@"showProfileView" sender:self];
}

ok log the user id you got from the segue in viewDidLoad of your profile viewController. also log the user returned from the query so we can see if you are actually getting anything from the backend

- (void)viewDidLoad
{
    [super viewDidLoad];

            //does this log a user id or null?
            NSLog(@"user id: %@", self.userId);
            PFUser *selectedUser = [PFQuery getUserObjectWithId:self.userId];
           //does this log a user object or null?
            NSLog(@"user object: %@", selectedUser);
            NSLog(@"username: %@" , selectedUser.username);
            self.userNameLabel.text = selectedUser.username; // Shows nothing
            self.emailLabel.text = selectedUser.email; //Shows nothing

}

also log the selectedUser id object in your prepareForSEgue method to amke sure it has a value when you pass it to the profile controller:

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

    if ([segue.identifier isEqualToString:@"showProfileView"]) {
        ProfileViewController *vc = (ProfileViewController *)segue.destinationViewController;
        //what does this log?
        NSLog(@"self.selectedUserId in prepareForSegue: %@", self.selectedUserId);
        vc.userId = self.selectedUserId; // could set this in didSelectRowAtIndexPath
    }
}

Interesting , my self.selectedUserId is null.

ok post your code where you actually set that property in didSelectRowAtIdexPath

I logged this too and it returned cE7mdCoUuA :)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
PFUser *user = [self.friends objectAtIndex:indexPath.row];
self.selectedUserId = user.objectId;
 NSLog(@"Selected User ID : %@" , self.selectedUserId);

}

hmm strange. I wonder why its being set to null. do you touch that property anywhere else in your code? ctrl f selectedUserId and see if you maybe set it to nil somewhere

I ctr f the friendsViewController and I found no occurrences where I set selectedUserId to null. This is strange , I'm wondering why .

I also found it interesting that the log in the segue came before the other logs(Including one in the didSelectRowAtIndexPath)

It works ! Thank you so much ! :)

awesome, glad you got it working : )

I seem to be having the same issue. I just cannot pass this value to my profileViewController no matter what i try. Which is odd because I've got several checks to see if the self.selectedUserId is being set and it recalls the correct Id in the FriendsVC, however it logs null in the profileVC every time.

here is my FriendsVC.m

import "FriendsViewController.h"

import "EditFriendsTableViewController.h"

@interface FriendsViewController ()

@end

@implementation FriendsViewController

  • (void)viewDidLoad { [super viewDidLoad];

    self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"]; }

  • (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

    PFQuery *query = [self.friendsRelation query]; [query orderByAscending:@"username"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if(error) { NSLog(@"Error: %@ %@", error, [error userInfo]); } else { self.friends = objects; [self.tableView reloadData]; } }]; }

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.friends count]; }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    PFUser *user = [self.friends objectAtIndex:indexPath.row]; cell.textLabel.text = user.username;

    return cell; }

  • (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; PFUser *user = [self.friends objectAtIndex:indexPath.row]; self.selectedUserId = user.objectId;

    NSLog(@" selected id - didSelect: %@", self.selectedUserId);

    [self performSegueWithIdentifier:@"showProfile" sender:self]; }

  • (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { /*NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; PFUser *user = [self.friends objectAtIndex:indexPath.row]; self.selectedUserId = user.objectId;

    NSLog(@" selected id - prepare: %@", self.selectedUserId); */

    if([segue.identifier isEqualToString:@"showEditFriends"]) { EditFriendsTableViewController *viewController = (EditFriendsTableViewController *)segue.destinationViewController;

    viewController.friends = [NSMutableArray arrayWithArray:self.friends];
    

    } else if ([segue.identifier isEqualToString:@"<showProfile>"]) { ProfileViewController *profileView = (ProfileViewController *)segue.destinationViewController; profileView.userId = self.selectedUserId; NSLog(@"self.selectedUserId in prepareForSegue: %@", self.selectedUserId); NSLog(@"Selected user in prepareForSegue: %@", profileView.userId); } }

@end

Here is my ProfileVC.m file

import "ProfileViewController.h"

import "FriendsViewController.h"

import <Parse/Parse.h>

@interface ProfileViewController ()

@end

@implementation ProfileViewController

  • (void)viewDidLoad { [super viewDidLoad];

    //does this log a user id or null? NSLog(@"user id: %@", self.userId);

}

  • (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; }

@end