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

发布于 2024-12-01 01:12:18 字数 143 浏览 0 评论 0原文

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

I've created a app that loads data from a XML feed file on my server. This works fine but I want it to refresh if the home button is pressed. I know that I need to use the UIApplicationDidBecomeActiveNotfication notif but I can't seem to get it to reload the feed. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2024-12-08 01:12:18

在具有重新加载方法的类中,您需要在初始化期间(或您想要开始观察的任何地方)添加一个观察者来观察通知,如下所示。您可以设置一个执行重新加载的选择器,我在这里使用了 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];
    }
}

如果您在初始化期间添加观察者,请务必在您的类被释放后将其删除。如果您将其添加到其他地方,则需要将其删除,否则如果您的类被释放并且观察者仍然处于活动状态,您的应用程序将崩溃

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

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

    [super dealloc];
}

现在,每次触发 UIApplicationDidBecomeActiveNotification 时,只要您的类存在,就会调用 reloadXMLData 方法是活跃的。

In the class which has the Reloading Methods, you need to add an Observer during init (or wherever you want to start observing) to observe for the notification like shown below. You can set a selector which does the reloading, i've used reloadXMLData here but you can change that to whatever.

- (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];
    }
}

Also be sure to remove the Observer once your Class is dealloced if you add it during init. If you add it somewhere else, you'll need to remove it otherwise if your class is dealloced and the observer is still active, your app will crash

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

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

    [super dealloc];
}

Now every time the UIApplicationDidBecomeActiveNotification is fired, the reloadXMLData method will be called as long as your class is active.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文