Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 接收UILocalNotification时从AppDelegate加载特定UIViewController_Ios_Objective C_Iphone_Uiviewcontroller_Uilocalnotification - Fatal编程技术网

Ios 接收UILocalNotification时从AppDelegate加载特定UIViewController

Ios 接收UILocalNotification时从AppDelegate加载特定UIViewController,ios,objective-c,iphone,uiviewcontroller,uilocalnotification,Ios,Objective C,Iphone,Uiviewcontroller,Uilocalnotification,我得到了一个UILocalNotification,它是在UIViewController的UIView部分内通过以下方式发送的: [[UIApplication sharedApplication] scheduleLocalNotification:_localNotif]; 我希望我的应用程序在以下情况下对此作出响应: 在后台:如果用户点击通知唤醒应用程序并加载相应的UIViewController(有多个UIViewController) 在前台时:刷新当前UIViewControl

我得到了一个UILocalNotification,它是在UIViewController的UIView部分内通过以下方式发送的:

[[UIApplication sharedApplication] scheduleLocalNotification:_localNotif];
我希望我的应用程序在以下情况下对此作出响应:

  • 在后台:如果用户点击通知唤醒应用程序并加载相应的UIViewController(有多个UIViewController)
  • 在前台时:刷新当前UIViewController中的数据
文档引导我在AppDelegate.m中修改此方法:

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

    NSLog(@"received notification");
    //reload views
}
但是,我不知道如何重新加载UIViewController

  • 我可以使用UILocalNotification的哪些属性来确定 你是不是发过来的
  • 如何从AppDelegate加载特定的UIViewController
我可以使用UILocalNotification的哪些属性来确定 你是不是发过来的

您可以将
UILocalNotification
userInfo
属性用于此特定目的。比如说:

localNotification.userInfo = @{ @"currentViewController" : self }; // put the instance of the current view controller into the userInfo dictionary
如何从AppDelegate加载特定的UIViewController

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

因此,例如,当您收到
UILocalNotification
时,代码可能看起来有点像这样:

- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"received_local_notification" object:nil];
}
以及要重新加载的
UIViewController
viewDidLoad
中:

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