从didReceiveRemoteNotification中更改视图

Chr*_*ker 1 iphone push-notification

作为iPhone开发的新手,我正慢慢地到达那里,但有时简单的事情似乎让我感到困惑.

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

我用 :-

DetailView *viewDetail = [[DetailView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:viewDetail animated:YES];
Run Code Online (Sandbox Code Playgroud)

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

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

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

在此先感谢Chris Hardaker

Mat*_*ham 12

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

DetailView *viewDetail = [[DetailView alloc] initWithNibName:nil bundle:nil];
Run Code Online (Sandbox Code Playgroud)

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

DetailViewController *viewDetail = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
Run Code Online (Sandbox Code Playgroud)

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

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

所以在didReceiveRemoveNotification:你可以打电话:

[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
Run Code Online (Sandbox Code Playgroud)

分配视图控制器时,请运行以下行:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationReceived:) name:@"pushNotification" object:nil];
Run Code Online (Sandbox Code Playgroud)

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

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

取消分配视图控制器时,请务必调用

[[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)