Question

I have a form that once a user submits, it should reload the view (but change a few things).

On this form there is a UIPickerview (_tripPicker) that has 2 locations a starting location and an end location (2 components to the pickerview).

I have it saving to the appropriate database and all that - but when it reloads (when the user clicks save) I want the pickerview to 'reset', and the first location (begSchool), to match the users second location (endSchool) that they just saved to coredata.

For example: Lets say user went from PointB to pointC (component 0 & 1 respectively in the pickerview); when they click save, I would like component 0 to display "PointC" and for the second component to just display the list of points to go to (resetting it to how it originally loads).

I've attempted to try and do some of this matching strings, but I'm running into issues 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 appropriate name of the school from component 1 of the pickerview, I just need to figure out how to match that with the appropriate value from the array that is loading the pickerview and make it selected in the pickerview. The text currently displays the appropriate name, but the pickerview does not show any change.

Please let me know if I need to clarify or what other code you need to see - thanks in advance.

Was it helpful?

Solution

I was able to solve this by first finding the indexValue in the array by matching the string against the Objects in the array.

I ended up with 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 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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top