I am just trying to transfer a simple string from a UILabel in a prototype cell into a label in the next View Controller. Value of label.text in the viewDidLoad of the View Controller is returning (null).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
mainCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (mainCell == nil) {
mainCell = [[dictionaryTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString* date = [dateArray objectAtIndex:indexPath.row];
mainCell.viewLabel.text = date;
return mainCell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"View Segue"]) {
NSLog(@"View Load Segue Success");
ViewController *one = segue.destinationViewController;
one.label.text = mainCell.viewLabel.text;
}
}
What am I doing wrong here?
You can use
indexPathForSelectedRow:Or you can also use
senderif your segue is from the cell to the next scene, e.g.:Two things to note:
As a matter of good programming style, I am not retrieving the text value from the cell. I’m retrieving the text value from the model. You should not be relying upon the view for information to be passed along. Go back to the model, the original source of the information.
Do not set the
textproperty of thelabelin the destination controller directly. The controls of thedestinationControllerhave not been created yet. You should defer setting controls until thedestinationController‘sviewDidLoad. So, instead, create aNSStringproperty in the destination controller:Clearly, you should use a more descriptive name than
textProperty, but hopefully you get the idea. Anyway,prepareForSeguecan set this new property and theviewDidLoadof the destination controller should then use thatNSStringproperty to populate thetextproperty of theUILabel, e.g.: