iosuitableviewdidselectrowatindexpathpoptoviewcontroller

popToRootViewController is called before viewWillAppear


I have a UITableviewController and when a row is selected, I want to pop back to my rootViewController and have it display the text of the row in table view. What is happening right now is the viewWillAppear in my regular view controller is being called before the popToRootViewController function is called, so when it goes to the regular view controller it does not display the text. I have tried using viewDidAppear also and it doesn't work. Here is the code I have tried:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSLog(@"row was selected");
    user[@"school"] = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];
    [[PFUser currentUser] saveInBackground];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

And then in the regular view controller:

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"will appear ran");
    PFUser *currentUser = [PFUser currentUser];
    self.userSchoolLabel.text = currentUser[@"school"];
}

What I see in the console is it prints out

"will appear ran"

and then "row was selected"

How can I fix this problem, also if i call the method popToRootViewController from the touch of a button on another view controller it works fine, the problem is with the selecting a row, I not sure why?

Thanks for your help in advance. I really appreciate it.


Solution

  • Event handling is a part of the Delegate methods. Your datasource seems to be correct as it is showing the row for you. So, may be you have not properly set the delegate of your tableview correctly. If you are using the nib then connect the delegate of your tableview or if you have use the code to draw the tableview simply check for the _yourtable.delegate =self; is set or not.

    Another reason could be if there is uiview in the tableview cell then it can take your rowselection event away.

    Also, if you are setting editing enabled in the tableview, it may not call the didSelectRowAtIndex: method... for detail see here