iphone - 如何在 Objective-C 上发送带参数的通知?

标签 iphone ios notifications

我需要发送带有参数toInterfaceOrientationduration 的通知@"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/

相关文章:

android - 点击后删除通知

iphone - 如何删除默认导航栏按钮

iphone - 将 UITableViewCell AccessoriesView 与单元格底部而不是中间对齐

ios - Swift - SecondViewController 到 FirstViewController : fatal error: unexpectedly found nil

iphone - iPad上的“可执行文件已使用无效的权利签名”,在iPhone上运行良好

ios - GPUImage 创建一个自定义过滤器来更改选定的颜色

iphone - 如何在-drawRect中绘制UILabel :?

iphone:将uiimage放在tableview部分标题中,拉伸(stretch)问题

notifications - RoR 上的 Google Calendar Watch API 方法存在问题

ios - 恢复应用程序时,ios 上的密码保护屏幕出现延迟