0

How do I call a method when my application comes back from the background?

I know there are methods that can be called in the app delegate, but I want to call a method in my view.

What is the best way of doing this?

Thanks in advance!

meth
  • 1,867
  • 2
  • 18
  • 33
George
  • 23
  • 5

1 Answers1

1

You should have your desired view controller register for UIApplicationWillEnterForegroundNotification notification, such as in initWithNibName:nibBundleOrNil:

- (id)initWithNibName(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethod:) name:UIApplicationWillEnterForegroundNotification object:nil];

        // Whatever else your init method should do here...
    }    

    return self;        
}

- (void)yourMethod:(NSNotification *)notification
{
    // whatever you want to do here...
}

Make sure you also unregister in dealloc:

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
JRG-Developer
  • 11,675
  • 8
  • 54
  • 79
  • Thanks both @JRG-Developer and @JustSid for your answers, but I'm still not having any luck. My issue is with UIImageView's not responding to touches after my application has reentered the foreground. I initially use a `touchesBegan` method, do I need to recall this somehow? I know this is moving to a new problem, but I hadn't realised this was my issue when I first asked the question. – George Apr 01 '13 at 18:02