从 didReceiveRemoteNotification 中更改视图

发布于 2024-12-07 17:10:17 字数 659 浏览 1 评论 0原文

作为 iPhone 开发的新手,我正在慢慢地实现这一目标,但有时简单的事情似乎难倒了我。

我的应用程序包含 7 个截然不同的视图。它是作为一个基于窗口的应用程序实现的,没有导航控制器。导航由每个视图控制器单独管理,我使用中央数据存储库来通过应用程序提供数据。

我用 :-

DetailView *viewDetail = [[DetailView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:viewDetail animated:YES];

来改变观点。显然,在所有情况下,这都是在当前视图的控制器内执行的。

但是,我现在已将推送通知添加到应用程序中。收到推送后,主 AppDelegate 中将提供 userInfo 数据,并执行 didReceiveRemoteNotification。我希望强制应用程序进入上述详细视图,并向其传递 userInfo 中的值之一。

当然,一旦执行第二行,我就会得到一个很好的 SIGABRT。我认为这是因为当前活动的视图不是 self。如何从应用程序委托中放弃当前视图以支持 DetailView?当前视图可以是 7 个视图中的任何一个,包括 DetailView,我想用新数据刷新它。

提前致谢 克里斯·哈德克

Being still new to iPhone development, I am slowly getting there however sometimes the simple things seem to stump me.

My application consists of 7 very different views. It is implemented as a window based app without a navigation controller. Navigation is managed by each view controller individually and I use a central data repository to make data available through the app.

I use :-

DetailView *viewDetail = [[DetailView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:viewDetail animated:YES];

to change views. Obviously, in all cases, this is executed within the controller of the current view.

HOWEVER, I have now added Push Notification to the app. Upon receipt of a Push, the userInfo data is made available within the main AppDelegate and didReceiveRemoteNotification is executed. I wish to force the application into the Detailview described above, passing it one of the values within userInfo.

Of course, I get a nice SIGABRT as soon as it executes the second line. I assume this is because the currently active view is not self. How do I surrender the current view in favour of the DetailView from within the app delegate? The current view could be ANY of the 7 views, including DetailView, which I wil want to refresh with the new data.

Thanks in advance
Chris Hardaker

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

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

发布评论

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

评论(1

苏辞 2024-12-14 17:10:17

这是一个小旁注,但您使用的命名约定是非标准的。按照

DetailView *viewDetail = [[DetailView alloc] initWithNibName:nil bundle:nil];

惯例,iOS 开发人员会期望看到以下内容:

DetailViewController *viewDetail = [[DetailViewController alloc] initWithNibName:nil bundle:nil];

因为 viewDetail 是 UIViewController 而不是 UIView 的子类。

为了回答你的主要问题,我会使用 NSNotificationCenter。基本上,这允许任何类发布通知,这或多或少只是抛出了一个事实:任何恰好正在侦听该事件的类发生了事件。

因此,在 didReceiveRemoveNotification: 中,您可以调用:

[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

当您分配视图控制器时,运行此行:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationReceived:) name:@"pushNotification" object:nil];

您将需要在每个视图控制器中放置一个名为 pushNotificationReceived: (NSNotification*)aNotification 的方法视图控制器来处理通知。您传递的 userInfo 字典将是通知的属性。

现在,当发送@“pushNotification”通知时,您的控制器将收到通知并运行给定的选择器。因此,您可以关闭控制器或显示详细视图或任何您想要的内容。

当您释放视图控制器时,请确保调用

[[NSNotificationCenter defaultCenter] removeObserver:self];

This is a small side note, but the naming convention that you are using is non-standard. Rather than,

DetailView *viewDetail = [[DetailView alloc] initWithNibName:nil bundle:nil];

conventionally, an iOS developer would expect to see the following:

DetailViewController *viewDetail = [[DetailViewController alloc] initWithNibName:nil bundle:nil];

since viewDetail is a subclass of UIViewController rather than UIView.

To answer you main question though, I would use NSNotificationCenter. Basically, this allows any class to post a notification, which more or less just throws out the fact that an event occurred to any class that happens to be listening for it.

So in didReceiveRemoveNotification: you can call:

[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

When you allocate your view controllers, run this line:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationReceived:) name:@"pushNotification" object:nil];

You will need to put a method called pushNotificationReceived: (NSNotification*)aNotification in each of the view controllers to handle the notification. The userInfo dictionary that you passed will be a property of the notification.

Now, when ever the @"pushNotification" notification is sent, your controllers will receive a notification and run the given selector. So you can dismiss the controller or show the detail view or whatever you want.

When you dealloc your view controller, make sure to call

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