I'm studying developing for iOS and while building a simple app which loads data from database and shows it as a tableview, I've got some issues which I fail to understand so far. The master - detail controllers' classes were created by me, not by the XCode template, if this matters.
I try to pass data from master tableview controller to detail controller. The data is as simple as a couple of strings. I use segue for this purpose. In
prepareForSegue
method I do the following:if ([segue.identifier isEqualToString: @"DetailsSegue"]) { DetailsViewController* dvc = (DetailsViewController*)segue.destinationViewController; NSInteger selectedRow =[self.tableView indexPathForSelectedRow].row; dvc.nameLabel.text = [NSString stringWithFormat:@"%@", [[self.entitiesArray objectAtIndex:selectedRow name]]; ... }
The problem here is that
dvc.nameLabel
isnil
. And I guess, that is possibly because the controller has not been fully created yet. Well, thedvc
pointer is notnil
, but I don't see the log in myinit
method, so my idea that it was not initialized.I decided to create an instance variable of DetailsViewController and in
prepareForSegue
set it:dvc->name = [NSString stringWithFormat:@"%@", [[self.entitiesArray objectAtIndex: selectedRow] name]];
and then I set
nameLabel
property inviewDidLoad
methodAnd it actually worked! So I guess I wouldn't be able to set instance variable of an unitialized instance. But I did. So what was wrong? And I feel this is not the way people do it, as to have one more variable that holds the same thing seems redundant.
So what is the proper way of passing a variable (in my case NSString) using a segue to another controller?
Thank you guys for help