0

当我的应用程序从后台返回时,如何调用方法?

我知道有可以在应用程序委托中调用的方法,但我想在我的视图中调用一个方法。

这样做的最佳方法是什么?

提前致谢!

4

1 回答 1

1

您应该让您想要的视图控制器注册UIApplicationWillEnterForegroundNotification通知,例如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...
}

确保您还取消注册dealloc

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
于 2013-04-01T17:30:42.823 回答