如何使用NotificationCenter UIApplicationDidBecomeActiveNotification重新加载我的RSS源

问题描述:

我已经创建了一个应用程序,用于从我的服务器上的XML提要文件加载数据。这工作正常,但我希望它刷新,如果主页按钮被按下。我知道我需要使用UIApplicationDidBecomeActiveNotfication notif,但我似乎无法得到它重新加载饲料。任何帮助将不胜感激。如何使用NotificationCenter UIApplicationDidBecomeActiveNotification重新加载我的RSS源

在具有重装方法的类中,您需要在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方法只要你的课程是活跃的,就会被调用。