Why is it that in the DetailViewController the NSLogs in viewDidLoad return Label and (null) respectively but in IBAction they return their expected values from the selected row? How can I get the proper values in viewDidLoad as well?
TableViewController
- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES];
NSUInteger row = [indexPath row];
detail.selected1.text = [array1 objectAtIndex:row];
detail.selected2 = [array2 objectAtIndex:row]; }
DetailViewController
-(void) viewDidLoad
{
NSLog(@"Log: %@", selected1.text);
NSLog(@"Log: %@", selected2);
}
- (IBAction) test:(id)sender
{
NSLog(@"Log: %@", selected1.text);
NSLog(@"Log: %@", selected2);
}
viewDidLoad gets called as a result of loading the view controller (i.e.instantiateViewControllerWithIdentifier:). So by the time you set your labels, viewDidLoad has already been run and has returned. Try putting your logging statements in viewDidAppear instead.