【问题标题】:is it possible to pass array as obj via nsnotification是否可以通过 nsnotification 将数组作为 obj 传递
【发布时间】:2012-07-24 14:46:35
【问题描述】:

我可以通过使用协议和委托来做到这一点,但我想尝试使用 NSNotification

我的任务是通过通知从一个视图向另一个视图发送NSMutableArray。有可能吗

[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:myArray];

那么,在receiver中,如何获取传递过来的myArray.我正在阅读并混淆通知对象的userInfoobject

请就这个问题给我建议。

【问题讨论】:

  • 是的,我确实尝试过,但是当我捕获传递的数组时。我应该使用[notification object] 并将其捕获到 NSMutableArray。 userInfo 怎么样。我什么时候应该使用 userInfo。

标签: iphone ios nsnotifications


【解决方案1】:

NSNotification 有一个名为userInfo 的属性,即NSDictionaryobject 是发布NSNotificationNSObject。所以通常我在设置NSNotification 时使用self 作为object,因为selfNSObject 发送NSNotification。如果您希望使用NSNotification 传递NSArray,我会执行以下操作:

NSArray *myArray = ....;
NSDictionary *theInfo =
  [NSDictionary dictionaryWithObjectsAndKeys:myArray,@"myArray", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData"
                                                    object:self
                                                  userInfo:theInfo];

然后使用以下命令捕获它:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(doTheReload:)
                                             name:@"reloadData"
                                           object:sendingObject];

其中sendingObject 是发送NSNotification 的对象。

最后,解码doTheReload: 中的数组:

NSArray  *theArray = [[notification userInfo] objectForKey:@"myArray"];

这总是对我有用。祝你好运!

【讨论】:

  • 非常快速的问题:当您说 where sendingObject is the object that is sending the NSNotification 时,所做的是创建 NSDictionary *sentOject = [NSDictionary alloc] init]; and pass it to [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doTheReload:) name:@"reloadData " sentObject]` 并且我根本没有收到通知....我是否错过了中间的东西
  • 不,sendingObject 是发布NSNotification 的对象。因此,如果它是由您的AppDelegate 发布的,例如,您需要将您的AppDelegate 放在object: 字段中。如果它是由某个 viewController 发送的,那么您需要将 THAT EXACT viewController 放在 object: 字段中。必须是发送NSNotification的对象的id
  • 刚刚修改,我做的是[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doTheReload:) name:@"reloadData" object:[A class]];我放了[A class],因为A是发布NSNotification的对象。但仍然不起作用......我正在考虑通过执行A *a = [[A alloc] init]来创建A类的对象,但我对此表示怀疑。 ..
  • 不行,你需要用self发送。您不传递发送它的对象的 CLASS,而是传递发送它的对象的实际 id,然后您需要捕获发送它的对象的实际 id。如果您不知道如何获取该信息,则可以使用nil,但我不建议您这样做。
  • @JustinPaulson 如果触发通知的控制器遵循不同的“故事板路径”并且不直接链接到捕获通知的事物,那么通过 appdelelgate 提供信息的最佳方式是什么?跨度>
【解决方案2】:

您应该使用userInfo。它用于您要与通知一起发送的杂项数据。 object 参数用于触发事件的对象。例如,如果您想监控某个 MPMoviePlayerController(而不是其他),那么您将只注册它的通知(通过 object 参数)。

【讨论】:

  • 如果他为自己发布通知,他可以同时使用objectuserInfo。只是他需要知道他用什么来做什么。 :)
  • userInfoNSDictionary。但是,我通过了NSMutableArray。就像你说的,我应该创建一个NSDictionary 来保存我的数组吗?
  • 我不知道为什么这被否决了。这正是正确的答案。您将 NSArray 存储在 userInfo 字典中。正如@borrrden 所说,object 应该始终是发布通知的对象。该数组没有发布通知,所以它不应该是object。未能始终遵循此约定是真正令人沮丧的错误的根源(通常具有奇怪的“不响应选择器”崩溃的症状)。
  • @ttran 是的,它是一个字典,可以方便地存储多条信息。将您的数组添加到字典(以及您想要的任何内容)
  • @borrrden:我正在决定是否应该创建一个dictionary 来保存我的object,例如myArray。现在我知道该怎么做了。谢谢
【解决方案3】:

这是非常一致的,因为当您使用 -postNotificationName:object:userInfo: 方法发布任何通知时,objectobjectuserInfouserInfo

是的,您可以通过NSNotificationCenter 发布NSObject 的任何子类。

【讨论】:

    【解决方案4】:

    斯威夫特 4、斯威夫特 5

    NotificationCenter.default.post(name: Notification.Name("favoriteIsDeleted"), object: [message, self.viewModel.deleteSuccessIcon])
    
    @objc func favoriteIsDeleted(notification: Notification) {
        guard let object = notification.object as? [String?], let message = object[0], let deleteSuccessIcon = object[1] else { return }
        // Code here ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 2016-11-20
      相关资源
      最近更新 更多