【问题标题】:loading a specific UIViewController from AppDelegate when receiving UILocalNotification接收 UILocalNotification 时从 AppDelegate 加载特定的 UIViewController
【发布时间】:2015-01-09 11:52:28
【问题描述】:

我得到了一个 UILocalNotification,它以这种方式在 UIViewControllerUIView 部分中发送:

[[UIApplication sharedApplication] scheduleLocalNotification:_localNotif];

我希望我的应用在以下情况下响应它:

  • 在后台时:如果用户点击通知唤醒应用并加载相应的 UIViewController(有多个 UIViewController)
  • 在前台时:刷新当前 UIViewController 中的数据

文档引导我在 AppDelegate.m 中修改此方法:

- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {

    NSLog(@"received notification");
    //reload views
}

但是我不知道如何重新加载 UIViewController。

  • 我可以使用 UILocalNotification 的哪些属性来确定哪些 UIViewController 发送了吗?
  • 如何从 AppDelegate 加载特定的 UIViewController?

【问题讨论】:

    标签: ios objective-c iphone uiviewcontroller uilocalnotification


    【解决方案1】:

    我可以使用 UILocalNotification 的哪些属性来确定哪些 UIViewController 发了吗?

    您可以将UILocalNotificationuserInfo 属性用于此特定目的。所以,类似:

    localNotification.userInfo = @{ @"currentViewController" : self }; // put the instance of the current view controller into the userInfo dictionary
    

    如何从 AppDelegate 加载特定的 UIViewController?

    您可以通过应用程序主UIWindowrootViewController 属性访问当前视图堆栈,该属性存储在AppDelegatewindow 属性中。如果您只想通知您的 UIViewController 收到本地通知,您可以发布 NSNotification 并将应作为观察者重新加载的视图控制器添加到特定的 NSNotification

    所以,例如当您收到UILocalNotification 时,代码可能看起来像这样:

    - (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"received_local_notification" object:nil];
    }
    

    还有viewDidLoad 中的UIViewController 中要重新加载的:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTheViewController:) name:@"received_local_notification" object:nil];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2014-11-27
      相关资源
      最近更新 更多