简体   繁体   English

打开通知后从AppDelegate重新加载ViewController

[英]Reload ViewController from AppDelegate after opening a Notification

I'm currently trying to reload my ViewController after opening a Notification from the notification center. 我目前正在尝试从通知中心打开通知后重新加载ViewController。 In my AppDelegate I'm using this function: 在我的AppDelegate中,我正在使用以下功能:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

if(application.applicationState == UIApplicationStateActive) {

    //app is currently active, can update badges count here

}else if(application.applicationState == UIApplicationStateBackground){


}else if(application.applicationState == UIApplicationStateInactive){

    //CODE

    //REFRESH VIEW HERE, AFTER THE CODE ABOVE

}

} }

How can this be done? 如何才能做到这一点? This is my first experience with Objective-C, so please don't be rude with me ihihihi. 这是我第一次使用Objective-C,所以请不要对我粗鲁。

Thanks. 谢谢。

You can use the NSNotificationCenter to let your UIViewController know you have some updated data to show. 您可以使用NSNotificationCenter来让UIViewController知道要显示的一些更新数据。

In the UIViewController register to the notification center for your specific notification and then update your view. UIViewController注册到通知中心以获取您的特定通知,然后更新视图。

In the app delegate: 在应用程序委托中:

[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"NEW_DATA_NOTIFICATION" 
    object:nil];

In your UIViewController , in the viewDidLoad : 在您的UIViewController ,在viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(receiveNewDataNotification:) 
    name:@"NEW_DATA_NOTIFICATION"
    object:nil];

Also in your UIViewController : 同样在您的UIViewController

- (void) receiveNewDataNotification:(NSNotification *) notification {
    if ([[notification name] isEqualToString:@"NEW_DATA_NOTIFICATION"]) {
        NSLog(@"Received New Data Notification!");
        // Update your view here...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM