Search code examples
objective-cios7storyboard

How to call viewWillAppear with storyboard iOS7


I'm using storyboard. As I remeber (I worked with ios 4, long time ago=)) everytime, when View appears, calls

-(void)viewWillAppear:(BOOL)Animated {}

method. Now this method doesn't call, if I press Home button and run app again.

How to fix it?

I need to update one UIView if it appears after home pressing.


Solution

  • The function viewWillAppear is not part of UIView. It is part of UIViewController.

    It is called after the view controller's view has been loaded and just before it starts to transition onto screen.

    If you create a subclass of UIView and put this function in it then it will never be called because it isn't supposed to be.

    Edit

    You are correct that viewWillAppear does not get called when the app is coming back from the background.

    If you want to update a part of your app when this happens then you can do something in the AppDelegate.

    I'd recommend not trying to store properties etc... in the AppDelegate though. You should do something like this...

    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    
        // just pass the message on. In your view you will need to add an observer for this notification
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateViewNotification" object:nil];
    }