Josue Espinosa
Josue Espinosa

Reputation: 129

Add text to an inactive viewcontroller's textfield

I have a list view of all the entities from my Core Data.

When tapped on, it should be pushing the values of the entity selected into the text fields in the view that is about to get pushed onto the navigation stack, but it doesn't.

What confuses and irritates me, is that if I use NSLog, it logs, but I use the same code and it doesn't output that same information into the textFields. the title also changes as it should, and it baffles me as to why the textfields dont. Am I missing something? Am I accessing the Athlete entity incorrectly?

Here is my code snippets (note Athlete.h is the dynamically create core data attributes, AllAthletes.h is the table view and AthleteDetail.h is the detail view getting pushed)

allathletes.h

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Athlete *athlete = (Athlete *)[athleteArray objectAtIndex:indexPath.row];
    AthleteDetail *athleteDetail= [self.storyboard instantiateViewControllerWithIdentifier:@"showAthlete"];
    athleteDetail.firstDetailTextField.text = athlete.first;
    athleteDetail.title = athlete.full;
    NSLog(@"Full Name: %@",athlete.full);
    NSLog(@"Parent's Full Name: %@", athlete.pfull);
    NSLog(@"Number: %@",athlete.phone);
    NSString *firstNameText = athleteDetail.firstDetailTextField.text;
    firstNameText = [NSString stringWithFormat:@"%@",athlete.first];
    [self.navigationController pushViewController:athleteDetail animated:YES];
    }

Upvotes: 0

Views: 67

Answers (1)

Shwet Solanki
Shwet Solanki

Reputation: 345

When you create an object for AthleteDetail AthleteDetail *athleteDetail= [self.storyboard instantiateViewControllerWithIdentifier:@"showAthlete"]; it does not initialise the controls subviewed.

The best way to get the desired the behaviour is to create a property named "athleteName" in AthleteDetail class

@property(nonatomic, strong) NSString * athleteName

now instead of athleteDetail.firstDetailTextField.text = athlete.first; write the following code

athleteDetail.athleteName = athlete.first;

now in viewDidLoad of AthleteDetail class, assign athleteName to your textfield

firstDetailTextField.text = _athleteName;

I hope this works for you.. :)

Upvotes: 2

Related Questions