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

时间:2011-08-19 23:08:55

标签: ios rss

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

1 个答案:

答案 0 :(得分:1)

在具有重新加载方法的类中,您需要在初始化期间(或者您想要开始观察的任何地方)添加一个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];
    }
}

如果在init期间添加了Class,请确保在释放Class后删除Observer。如果你把它添加到其他地方,你需要删除它,否则如果你的类被解除分配并且观察者仍处于活动状态,你的应用程序将崩溃

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

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

    [super dealloc];
}

现在每次触发UIApplicationDidBecomeActiveNotification时,只要您的类处于活动状态,就会调用reloadXMLData方法。