1

我一直在尝试一切工作。当应用关闭2个自定义项目,类型和ID时,我会收到通知。该类型应该告诉我要加载哪个视图,并且该id应该告诉应用程序从数据库中获取哪一行。我正在试图解决这个问题。应用程序didreceiveRemoteNotification和跳转到特定视图

我需要点击通知,并让我带到相关记录。到目前为止,我已经用两种不同的方法取得了成功,我将在下面概述。

我还要指出的是,我所知道的有效载荷从APNS正常工作,因为我已经是调试死亡:)

我想的第一件事就是如下:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 

    NSString *itemType = [[userInfo objectForKey:@"T"] description]; 
    NSString *itemId = [[userInfo objectForKey:@"ID"] description]; 

    self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    // type 1 = call, type 2 = contact 
    if ([itemType isEqual: @"1"]) { 
     Leads_CallsDetailViewController *callView = [[Leads_CallsDetailViewController alloc] init]; 
     [callView displayItem:itemId]; 
     [self.window addSubview:callView.view]; 
     [self.window makeKeyAndVisible]; 
    } else if([itemType isEqual: @"2"]) { 
     Leads_ContactsDetailViewController *contactView = [[Leads_ContactsDetailViewController alloc] init]; 
     [contactView displayItem:itemId]; 
     [self.window addSubview:contactView.view]; 
     [self.window makeKeyAndVisible]; 
    } 
} 

随着这一个,我有一个名为displayItem的详细视图的方法,我将用它从api获取数据,然后显示它。这做了一些事情,但它看起来像从未真正加载的视图。我在页面上有一个滚动视图和各种按钮,但是从addSubview加载的所有内容都是背景图片。没有任何事情真的发生完全加载视图。我不知道该如何处理。

我试过的第二件事是直接去像这样的观点:

NSString *storyboardId = @"Leads_Calls_SB"; 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:storyboardId]; 
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
self.window.rootViewController = initViewController; 
[self.window makeKeyAndVisible]; 

这一个似乎加载视图功能和漂亮的有两个主要注意事项期待。 1.我不确定如何将数据传递给它,以及2.当我试图回弹时它不喜欢它,当我尝试从那里继续推进时它也很生气,就好像没有导航一样即使整个应用程序嵌入在导航控制器中,也是如此。

非常感谢您的帮助。如果有人能帮我弄清楚这一点,我会很感激你。

回答

5

通常情况下这一要求,我会做这个..

  1. 使用NSNotificationCenter和后从didReceiveRemoteNotification的通知。

    [[NSNotificationCenter defaultCenter] postNotificationName:@"notificationReceived"  object:self userInfo:userInfo]; 
    
  2. 从VC订阅它可以打开您的详细信息视图以显示消息。

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceived:) name:@"notificationReceived" object:nil]; 
    
  3. 如果你自己实例化VC并且不使用segue。你可以做到这一点..

    UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    detailVC = [storyBoard instantiateViewControllerWithIdentifier:@"detailVC"]; 
    detailVC.delegate = self; 
    
    detailVC.userInfo = @"YOUR DATA"; 
    [self presentViewController:detailVC animated:YES completion:nil]; 
    
  4. 要返回您可以在您的详细VC做..

    [self dismissViewControllerAnimated:YES completion:nil]; 
    
+0

酷。我明天将会处理这个问题,并且还有一个问题。我试图在NSNotificationCenter上阅读,我很困惑这到底是什么。当我点击通知时,我想创建一个名称为notificationReceived的通知中心,是否正确?然后从那里,我创建一个监听器在我的详细信息视图。然后我介绍详细视图。 NSNotificationCenter中实际的数据传递如何发生?或者我只是从userInfo传入数据?如果后者,那么我需要NSNotificationCenter。对不起,如果这是一个痛苦,只是想明白! :D –

+0

嗨克里斯,NSNotificationCenter有一个发布者订阅模式。你可以使用userInfo参数传递数据。 NSNotificationCenter的美妙之处在于,它使得你的应用程序松散耦合并传递数据,而无需在发送者和接收者之间直接引用。 – travoux

相关问题