I’m having a strange issue with updating the U.I. after a segue. It’s a pretty standard design: the user selects a cell in a tableview, this triggers a segue to a detail screen which reflects the values of the object–in this case, a reed–in the selected cell and allows the user to edit.
I have a picker view in the detail screen with two components, an index number and a brand.
When I click a cell, the segue goes but the U.I. does not update–the picker stays in the default state, index 0.
If I hit “cancel” to go back to the table view, then click the same cell again, the detail view WILL update properly.
If I then hit cancel and select a new cell, the detail view still reflects the last cell clicked. If I click it again, it updates properly. You get the picture: I have to click any given cell twice in order to have it update.
EDIT: I realized that I was calling the segue twice, in the storyboard and manually via code. I deleted the line of code that manually called performSegue…now the object is only being passed once, but it still does not update on the first try.
The picker is allocated (with no values) in ViewDidLoad, if that makes a difference. Then it is supposed to be updated in ViewDidAppear.
my fetchedResultsController’s cache name is initialized to “nil.” This means there is no cache, right? So that’s not the issue?
Here’s the relevant code from the table view. reedToEdit is a property on this class, which is passed to the detail controller (which has a property of the same name):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.reedToEdit = [fetchedResultsController objectAtIndexPath:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *navigationController = segue.destinationViewController;
ReedDetailsViewController *controller = (ReedDetailsViewController *) [[navigationController viewControllers] objectAtIndex:0];
if ([segue.identifier isEqualToString:@"AddReed"]) {
controller.reedToEdit = nil;
controller.managedObjectContext = self.managedObjectContext;
controller.delegate = self;
} else if ([segue.identifier isEqualToString:@"ShowReed"]) {
controller.reedToEdit = self.reedToEdit;
NSLog(@"*** prepareForSegue. reedToEdit = %@", controller.reedToEdit);
NSLog(@"controller = %@", controller);
controller.title = @"Edit Reed";
controller.managedObjectContext = self.managedObjectContext;
controller.delegate = self;
} else {
NSLog(@"***Unrecognized segue");
abort();
}
}
And here’s the code from the detail controller (with the picker view).
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (self.reedToEdit != nil) {
isEditing = YES;
[self updateInterface];
}
}
-(void)updateInterface
{
[picker selectRow:[self.reedToEdit.indexNumber intValue] -1
inComponent:1
animated:NO];
[picker selectRow:[self rowForBrand: self.reedToEdit.brand]
inComponent:0
animated:NO];
}
I see several issues.
First, the proper way to pass a parameter to your segue based in the selected index path is not setting an ivar in
didSelectRowAtIndexPath:and using this ivar inprepareForSegue:. Rather, you should be using thesenderobject.BTW, you can avoid the redundancy of lines like
by putting them outside the
ifclause distinguishing the segues.Second, what is mainly a cosmetic issue is the fact that you are setting a property of the picker without animation in
viewDidAppear:As a rule of thumb you should not use the animation if the control is off-screen, otherwise animation is in order. The proper place to set the value of your UI elements is inviewWillAppear:or even inviewDidLoad:, i.e. before they appear.