How do you share data between view controllers and other objects in Swift?

后端 未结 8 1144
长情又很酷
长情又很酷 2020-11-22 02:15

Say I have multiple view controllers in my Swift app and I want to be able to pass data between them. If I\'m several levels down in a view controller stack, how do I pass d

8条回答
  •  野的像风
    2020-11-22 02:55

    Another alternative is to use the notification center (NSNotificationCenter) and post notifications. That is a very loose coupling. The sender of a notification doesn't need to know or care who's listening. It just posts a notification and forgets about it.

    Notifications are good for one-to-many message passing, since there can be an arbitrary number of observers listening for a given message.

提交回复
热议问题