1

我一直在努力解决这个问题。当应用程序使用 2 个自定义项、一个类型和一个 ID 关闭时,我会收到通知。类型应该告诉我要加载哪个视图,id 应该告诉应用程序从数据库中获取哪一行。我正在经历地狱试图解决这个问题。

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

我还应该指出,我知道有效负载在 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.当我试图弹回时它不喜欢它,当我试图从那里继续推送时它也很生气,几乎就像没有导航一样视图控制器,即使整个应用程序嵌入在导航控制器中。

非常感谢你的帮助。如果有人能帮我解决这个问题,我会感激你的。

4

1 回答 1

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];
    
于 2013-07-06T02:15:43.733 回答