在iOS中发送通知的可能性是什么



我想知道发送通知的可能性是什么。可以发送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

在发布它的类中:

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获取

你不能发送NSUserDefault,但你可以发送整个类(存档器/取消存档器)数据,例如在base64中转换。接下来从NSData创建您的NSUserDefault

我写了一篇文章,使用base64与应用程序交换数据并使用它。

http://www.albertopasca.it/whiletrue/2012/04/objective-c-share-classes-objects-apps/

希望这有帮助。

最新更新