Select UIPickerview based on string match from coredata

I have a form that the user submits, he must reload the view (but change a few things).

This form has a UIPickerview (_tripPicker) in which there are 2 locations, a source location and an end location (2 components to choose from).

I have it saved in the appropriate database and all that - but when it reboots (when the user clicks on save), I want pickerview to be "reset" and the first place (begSchool) to match the users of the second location (endSchool) they just saved in coredata.

For example: Assume that the user has moved from PointB to pointC (components 0 and 1 respectively in pickerview); when they click "Save", I would like component 0 to display "PointC", and for the second component, just display a list of points that you need to go to (resetting it before it initially loads).

I tried to execute some of the relevant lines, but I have problems with it.

Here is the logic where I do this:

//Save the data & reload View
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {

//Reset the pickerview  **********THIS LOGIC NEEDS WORK**********
//Check to see if there is previous UserMiles entered - if so, set beg_school to appropriate name
// Get the local context
NSArray *tripsSorted = [UserMiles MR_findAllSortedBy:@"driven_date" ascending:NO];
if (!tripsSorted || !tripsSorted.count){
    //if no previous trips have been entered - set the school list to default
    begSchoolLabel.text = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]];

} else {
    UserMiles *lastTrip = [tripsSorted objectAtIndex:0];
    NSString *preValue = lastTrip.end_school;
    begSchoolLabel.text = preValue;
    NSUInteger *currentIndex = [_schoolArray1 indexOfObject:preValue]; //Error/warning that incompatible integer to point conversion
    [_tripPicker selectRow:currentIndex inComponent:0 animated:YES];  //Error/warning here that incompatible point to integer conversion
    NSLog(@"LastTrip.endSchool = %@", lastTrip.end_school);
}

//Set end school labels for the second component in the picker
endSchoolLabel.text = [_schoolArray2 objectAtIndex:[_tripPicker selectedRowInComponent:1]];

NSLog (@"saveInBackground: finished!");

}];
[self.view setNeedsDisplay];

lastTrip.end_school gets me the corresponding school name from pickerview component 1, I just need to figure out how to match this with the corresponding value from the array that pickerview loads and makes it selected in pickerview. The text currently displays the corresponding name, but pickerview doesn't show any changes.

, , - .

+3
1

, indexValue , .

:

//Save the data & reload View
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {

//Reset the pickerview  **********THIS LOGIC NEEDS WORK**********
//Check to see if there is previous UserMiles entered - if so, set beg_school to appropriate name
// Get the local context
NSArray *tripsSorted = [UserMiles MR_findAllSortedBy:@"driven_date" ascending:NO];
if (!tripsSorted || !tripsSorted.count){
    //if no previous trips have been entered - set the school list to default
    begSchoolLabel.text = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]];

} else {
    UserMiles *lastTrip = [tripsSorted lastObject];
    NSString *preValue = lastTrip.end_school;
    begSchoolLabel.text = preValue;
    int indexValue = [_schoolArray1 indexOfObject:preValue]; //Compares the preValue string to the strings in the array and finds the indexValue of the right match
    [_tripPicker selectRow:indexValue inComponent:0 animated:YES]; //Sets the picker to the appropriate value
    NSLog(@"LastTrip.endSchool = %@", lastTrip.end_school);
}

//Set end school labels for the second component in the picker
endSchoolLabel.text = [_schoolArray2 objectAtIndex:[_tripPicker selectedRowInComponent:1]];

NSLog (@"saveInBackground: finished!");

}];
[self.view setNeedsDisplay];
+1

All Articles