ios - 解析推送通知,收到通知时如何修改当前 View Controller 中的变量?

标签 ios xcode uiviewcontroller parse-platform

当我的应用收到推送通知时,我想增加一个变量,即 totalMessages++。我知道:

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

     [PFPush handlePush:userInfo];
 }

在收到推送且应用程序当前打开时调用。然而,这是在 AppDelegate.m 中声明的。我将如何修改当前显示的 View Controller 中的变量,即 FriendDisplayViewController?

最佳答案

您可能希望自己处理通知和负载(阅读:userInfo),例如:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  [[NSNotificationCenter defaultCenter] postNotificationName:superUniqueNotificationName
                                                      object:nil
                                                    userInfo:userInfo];
}

为了让 appDelegate 和您的 viewController 使用相同的 notificationName,您需要在某个地方集中共享它(例如:AwesomeConstants.h/.m):

FOUNDATION_EXTERN NSString * const superUniqueNotificationName; // .h

NSString * const superUniqueNotificationName = @"superUniqueNotificationName"; // .m

让你的 viewController 包含类似的东西

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

-(void)dealloc {
  [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)receivedParseNotification:(NSNotification *)parseNotification {
  NSLog(@"got %@ in FriendDisplayViewController, let's rock some variables", parseNotification);
}

关于ios - 解析推送通知,收到通知时如何修改当前 View Controller 中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28069796/

相关文章:

ios - 如何禁用 UIApplication.keyWindow() 呈现的 ViewController 上的方向

ios - 使用自动布局将多个 UIButtons 作为 subview 添加到 UIview

ios - 使用 native Facebook 帐户(在设置中)登录 Facebook,无需网页 View

ios - 网络电话 XCode 8、Swift 3 和 iOS 9.x

objective-c - 如何在 objective-c++ 中编译特定文件以及在 objective-c 中编译项目的其余部分

iOS UINavigationController - 从导航堆栈中删除 View Controller 确实会释放它

ios - 多行容器 View 根据宽度布置子项

objective-c - Xcode - 如何创建随主窗口移动的粘性面板窗口?

swift - 如何在 Xcode 9 中实现 PKPaymentAuthorizationViewControllerDelegate 以实现 iOS 8 向后兼容?

swift - 在应用程序委托(delegate)中识别和加载 View Controller 时出现“使用无法解析的标识符 'rootViewController'”错误