2011-08-19 40 views

回答

1

在具有重装方法的类中,您需要在init(或您想要开始观察的任何地方)期间添加Observer,以观察如下所示的通知。你可以设置一个重载的选择器,我在这里使用了reloadXMLData,但你可以改变它。

- (id)init { 
    self = [super init]; 
    if (self) { 
     // Other init code here... 

     // Add our Observer 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadXMLData) name:UIApplicationDidBecomeActiveNotification object:nil]; 
    } 
} 

如果您在初始化过程中添加了Class,那么一旦您的Class被处理,也一定要删除Observer。如果添加了别的地方,你需要,如果你的类是dealloced和观察者仍然活跃于否则就删除它,您的应用程序会在每次UIApplicationDidBecomeActiveNotification触发崩溃时

- (void)dealloc { 
    // Other dealloc code here... 

    // Remove our Observer 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 

    [super dealloc]; 
} 

现在,reloadXMLData方法只要你的课程是活跃的,就会被调用。