从didReceiveRemoteNotification内部更改视图 [英] Changing view from within didReceiveRemoteNotification

查看:46
本文介绍了从didReceiveRemoteNotification内部更改视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是iPhone开发的新手,但是我渐渐步入正轨,但有时简单的事情似乎使我难过.

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

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

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.

我使用:-

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.

但是,我现在已经向应用程序添加了推送通知.收到Push消息后,将在主AppDelegate中使userInfo数据可用,并执行didReceiveRemoteNotification.我希望将应用程序强制传递到上述的Detailview中,并将其传递给userInfo中的值之一.

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.

当然,一旦执行第二行,我就会得到一个不错的SIGABRT.我认为这是因为当前活动视图不是self.如何在应用程序委托中让当前视图支持DetailView?当前视图可能是7个视图中的任何一个,包括DetailView,我将要使用新数据刷新该视图.

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.

先谢谢了 克里斯·哈达克(Chris Hardaker)

Thanks in advance Chris Hardaker

推荐答案

这是一个小注释,但是您使用的命名约定是非标准的.而不是

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

通常,iOS开发人员希望看到以下内容:

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

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

因为viewDetail是UIViewController的子类,而不是UIView.

since viewDetail is a subclass of UIViewController rather than UIView.

尽管要回答您的主要问题,我将使用NSNotificationCenter.基本上,这使任何类都可以发布通知,或多或少会抛出一个事实,即任何正在监听该类的类都发生了事件.

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.

因此在didReceiveRemoveNotification:中,您可以致电:

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

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

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.

现在,一旦发送@"pushNotification"通知,您的控制器将收到一条通知并运行给定的选择器.因此,您可以关闭控制器或显示详细视图或任何您想要的东西.

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

这篇关于从didReceiveRemoteNotification内部更改视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆