我想知道发送通知的可能性有多大。
是否可以发送 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/