popviewcontroller is not calling viewWillappear

10,776

Solution 1

Make sure your navigation controller's delegate is set and then use this function to call viewWillAppear in the class whose viewWillAppear you want to call:

- (void)navigationController:(UINavigationController *)navigationController 
      willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
    [self viewWillAppear:animated];
}

Solution 2

I've just hit a problem very much the same and after some testing discovered that calling popViewControllerAnimated: in a block (from a network response in AFNetworking) viewDidAppear isn't called in the parent view. The solution that worked for me here was to call it in the main thread instead.

dispatch_async(dispatch_get_main_queue(), ^{
    // If not called on the main thread then the UI doesn't invoke the parent view's viewDidAppear
    [self.navigationController popViewControllerAnimated:YES];
});
Share:
10,776
atbebtg
Author by

atbebtg

Updated on June 09, 2022

Comments

  • atbebtg
    atbebtg almost 2 years

    I'm using the following code to display the previous view when a user is clicking on a button

    [self.navigationController popViewControllerAnimated:YES];
    

    In the previous view, I overwrite viewWillAppear to initialized few things. However, it seems like viewWillAppear is not being called. I put NSLog in viewDidload, viewWillAppear, viewDidAppear and only viewDidAppear is being called. Is this normal behavior? If yes, what event should I override so I can do my initialization? Thank you.

    As requested -viewWillAppear for the previous view

    - (void)viewWillAppear:(BOOL)animated{
    
        NSLog(@"ViewWillAppear");
            //[[GameStore defaultStore] resetGame];
            [self setHangmanImage];
    
        NSLog([[[GameStore defaultStore] selectedList] label]);
            [labelListName setText:[NSString stringWithFormat:@"List Name: %@", [[[GameStore defaultStore] selectedList] label]]];
            [labelCurrentIndex setHidden:YES];
            [labelCurrentWord setHidden:YES];
            [[self navigationController] setNavigationBarHidden:NO];
    
            [FlurryAnalytics logEvent:@"GameViewController - viewWillAppear"];
    
            [self getNewQuestion];
    
        NSLog(@"ViewWillAppear finish");
        [super viewWillAppear:YES];
    
    }
    

    I setup the UINavigationalController in the app delegate using the following code

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        HomeViewController *hv = [[HomeViewController alloc] init];
    
        UINavigationController *navController = [[UINavigationController alloc]
                                                 initWithRootViewController:hv];
    
        // You can now release the itemsViewController here,
        // UINavigationController will retain it
        [hv release];
    
        // Place navigation controller's view in the window hierarchy
        [[self window] setRootViewController:navController];
    
    
        [navController release];
    
    
        // Override point for customization after application launch.
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    UPDATE

    I don't know what happened but last night after trying to run the app one more time in the simulator and its still having this issue, I decided to save everything and shut my computer down since it was getting late.

    This morning I turned my computer back on opened up xcode, clean the project and build and run it and I the problem is fixed and -viewWillAppear is called. I didn't change anything and its working. I added NSLog in -willShowView and its not getting called. I don't know why all of a sudden viewWillAppear is being called.

  • atbebtg
    atbebtg over 12 years
    I did this and the willShowViewcontroller is not being called.
  • Bushra Shahid
    Bushra Shahid over 12 years
    is the navigation controller's delegate set? does this class have <UINavigationControllerDelegate> in its header?
  • atbebtg
    atbebtg over 12 years
    yes, I tried to do it in both the previous view and the view that call the popViewController and its not being called in both place