ios - 在 iOS 中发送通知有哪些可能性

标签 ios uiviewcontroller nsuserdefaults apple-push-notifications nsnotificationcenter

我想知道发送通知的可能性有多大。 是否可以发送 NSUserDefaults

我知道你可以发送另一个 viewcontroller

像这样:

NSUserDefaultsDidChangeNotification 只是在默认设置更改时发出的通知。要收听它,您需要此代码:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
           selector:@selector(defaultsChanged:)  
               name:NSUserDefaultsDidChangeNotification
             object:nil];

这将在触发通知时调用方法 defaultsChanged:。你需要像这样实现这个方法:

- (void)defaultsChanged:(NSNotification *)notification {
 // Get the user defaults
NSUserDefaults *defaults = (NSUserDefaults *)[notification object];

// Do something with it
NSLog(@"%@", [defaults objectForKey:@"nameOfThingIAmInterestedIn"]);
}

最佳答案

嗯,

这里可以使用

通过NSNotificationCenter发送字典
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo

在您发布它的类(class)中:

NSDictionary *dict;

dict = [NSDictionary dictionaryWithObjectsAndKeys: yourStuff, nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@”someString” object:nil userInfo:dict];

在类里面做听力:

[[NSNotificationCenter deHaultCenter] addObserver:self selector:@selector(someMethod: ) name:@”someString” object:nil];
…
- (void)someMethod:(NSNotification *)notification {
NSDictionary *tmp = notification.userInfo;
//You could access notification.object here too
}

编辑: 但通常在从服务器接收推送通知时,你有一个方法叫做:

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }
}

在这个方法中,你也可以获得作为Dictionary的payload

关于ios - 在 iOS 中发送通知有哪些可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12454764/

相关文章:

ios - 为了开始全职 IOS 开发,我需要知道什么?

防止 CRLF 的 iOS Base64 Lib

ios - Objective-c 项目 : not supported for static libraries 中的 Swift

objective-c - 使用 ARC 从导航 Controller 弹出时保留 View Controller

ios - 当我关闭模态呈现的 viewController 时,我的 GameScene 卡住

ios - 存储 NSUserDefaults 时访问错误

ios - 什么时候使用?、!、无或惰性?

ios - 将 definesPresentationContext 与 UIModalPresentationStyle.custom 结合使用

objective-c - NSUserDefaults 正确存储 NSDate,然后在第二次检索后返回 null

iOS UILocalNotification 设置为每天触发一次,每两天触发一次,并且仅在星期日触发