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
Robert Maidla
15,612 PointsProblem with setting a property of another class.
Hi! I'm having difficulties setting a property of another class.
Basically I have this tableViewController which contains rows of friends. Once a row is selected, the user is directed to another viewController where I want to display additional information about the user. In the detailed viewController there is a strong, nonatomic property called *selectedUser of class PFUser.
Now in the tableViewController I've added the didSelectRowAtIndexPath method which looks like this:
-
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
PFUser *user = [self.friends objectAtIndex:indexPath.row]; FriendDetailsViewController *FDViewController = [[FriendDetailsViewController alloc] init]; FDViewController selectedUser = user; NSLog(@"Selected user in tableview: %@", user.username);
}
..where the self.friends property is an NSArray containing an array of PFUsers. When I log the user.username at the end of the method, it returns the correct value (the selected rows user username) but when I log the 'selectedUser' properties value 'username' in the detail viewController, it returns nil. Why isn't the property I set from the tableViewController method sent over to the detail viewController?
2 Answers
eirikvaa
18,015 PointsYou should pass data from the first view controller to the second view controller in the prepareForSegue method. There you can get a valid reference to the second view controller like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SecondViewController *secondViewController = (SecondViewController *)segue.destinationViewController;
secondViewController.property = someProperty;
}
In your case the sender parameter would refer to the cell you selected, so you can use that when setting the property for the second view controller.
As for why the property is returning nil in the second view controller, I guess this has something to do with the fact that you're just creating a new reference to the second view controller. Sure, it is valid, but it isn't a reference to the view controller you're about to segue to.
Robert Maidla
15,612 PointsI actually had a typo in my code regarding the second type of property setting.
SecondViewController.property = someProperty; - is actually the correct way if the property types match. Thank you again for the help.
eirikvaa
18,015 PointsGreat! No problem.
Robert Maidla
15,612 PointsRobert Maidla
15,612 PointsThank you for your help! I managed to pass on the string variables to the other class but couldn't pass a PFUser property for some reason..I was getting an 'unknown identifier' error while trying to use either:
SecondViewController.property = [PFUser someProperty];
or
SecondViewController.property = someProperty
eirikvaa
18,015 Pointseirikvaa
18,015 PointsWould you mind posting the whole error message?