我需要发送带有参数toInterfaceOrientation
和duration
的通知@"willAnimateRotationToInterfaceOrientation"
(问题 #1) 到应用程序上的所有 UIViewController
(问题 #2)。如何为此编写代码?
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(willAnimateRotationToInterfaceOrientation:toInterfaceOrientation:duration)
name:@"willAnimateRotationToInterfaceOrientation"
object:nil];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"willAnimateRotationToInterfaceOrientation"
object:self];
最佳答案
使用 postNotificationName:object:userInfo:
并将您希望传递到 userInfo
字典中的任何参数捆绑在一起。
例子:
你可以这样发布通知
NSDictionary * userInfo = @{ @"toOrientation" : @(toOrientation) };
[[NSNotificationCenter defaultCenter] postNotificationName:@"willAnimateRotationToInterfaceOrientation" object:nil userInfo:userInfo];
然后通过执行以下操作检索您传递的信息:
- (void)willAnimateRotationToInterfaceOrientation:(NSNotification *)n {
UIInterfaceOrientation toOrientation = (UIInterfaceOrientation)[n.userInfo[@"toOrientation"] intValue];
//..
}
总结一下上面看到的内容,用于处理通知的选择器采用一个类型为 NSNotification
的可选参数,您可以将要传递的任何信息存储在 userInfo< 中
字典。
关于iphone - 如何在 Objective-C 上发送带参数的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15957686/