0

I have a UINavigationController hierarchy, VC1 --> VC2.

VC1 has a table view that I need to reload when VC2 is done with its work, so VC1 has this code:

-(void)viewWillAppear:(BOOL)animated
{
    [[self tableView] reloadData];
}

VC2 is essentially working with the server to create a new table row in VC1. When the done button in VC2 is pressed, I call [navController popViewControllerAnimated:YES]. So here's what happens from the user's perspective:

  1. Visit VC2, use it to create a new row for the table in VC1. Press done.

  2. The hierarchy successfully navigates back to VC1, but the tableview does not reload and display the new row.

  3. However, if I then nav forward to VC2, and immediately hit the navController back button, the table does reload and show the new row.

So why does [tableview reload] work on 3 but not 2? Thanks so much.

==

More code in response to answer mentioned below:

In App delegate:

    CWLandingVC *lvc = [[CWLandingVC alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:lvc];
    [[self window] setRootViewController:navController];

In VC0:

-(void)toSessionMgmtViewController
{
    TSessionMgmtViewController *tsmvc = [[TSessionMgmtViewController alloc] init];
    [[self navigationController] pushViewController:tsmvc animated:YES];
}

In VC1:

- (IBAction)toCreateSessionView:(id)sender
{
    TCreateSession *cs = [[TCreateSession alloc] init];
    [[self navigationController] pushViewController:cs animated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[self tableView] reloadData];
}

In VC2: Finishes working with server...

    UINavigationController *navControler = [self navigationController];
    [navControler popViewControllerAnimated:YES];

Also, when VC2 is done working with the server, it updates a data store of TSessions called SessionListStore:

 - (TSession *)addSession:(NSString *)code withName:(NSString *) name qs:(int)qs
 {
     TSession *s = [[TSession alloc] initWithName:name code:code numberQuestions:qs];
     [_sessions setObject:s forKey:code];
     return s;
 }

where sessions is a NSNutatbleDictionary in SessionListStore.

Thanks so much in advance.

Community
  • 1
  • 1
roro
  • 265
  • 3
  • 11

1 Answers1

0

EDIT: The solution was to trigger the reloadData call from the completion block of Server call.

Please check this answer,

Popping ViewController doesn't call viewWillAppear when going back

Have you added navigation controller to your view controller or view controllers to your navigation controller?

Also, you can set the desired view controller as the delegate of your navigation controller and implement this method.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
Community
  • 1
  • 1
ManicMonkOnMac
  • 1,476
  • 13
  • 21
  • Thanks, though unlike the scenario mentioned there, I am not nesting a UITabBarController in a UINavigationController. So I believe I am already doing what that answer suggests. Any other reason viewWillAppear isn't being called? – roro Oct 28 '13 at 14:02
  • did you check out this link on the above mentioned post? http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/ – ManicMonkOnMac Oct 28 '13 at 14:25
  • Ok Thx, so that post says "...if you add a UINavigationController as a subview of a UIViewController subclass, you must explicitly call its viewWillAppear". But in my project, nav controller is not a subclass of another vc (see newly added app delegate code above). Am I misunderstanding something still? Thanks, – roro Oct 28 '13 at 15:22
  • add a break point in VC1's viewWillAppear: and check if it's being called, I bet it is. Also, can you post the code that adds data to your table view? – ManicMonkOnMac Oct 28 '13 at 15:47
  • Ah yes, you are correct. I added a breakpoint in viewWillAppear and it gets called. But the table in VC1 still doesn't show the new cell. Then if I do the -->VC2 <--Back step, it shows up. Sure I posted the bit that updates my SessionListStore, from which the table view is generated. – roro Oct 28 '13 at 16:41
  • does it show up even when you don't press the done button first? try this, remove the popViewController in your VC2, and instead print a log "when its done with server" and then hit back. I think what's happening is your data is not yet available when viewDidAppear is called at popViewController, and when you press back button after going to VC2, the data is available. You should try and use a block, and and in the completion block send the message to your VC1 for reloadData. – ManicMonkOnMac Oct 28 '13 at 16:55
  • MMoM thanks for your help, you are totally right. I can't believe I was not triggering from the server response block. I feel like (am) a newb. Thanks! – roro Oct 28 '13 at 18:55
  • no problem bro, I have edited the answer above to include the solution as reached. – ManicMonkOnMac Oct 28 '13 at 19:34