How to update a SwiftUI view state from outside (UIViewController for example)

前端 未结 2 2042
既然无缘
既然无缘 2021-01-03 22:39

I have a SwiftUI view:

struct CatView : View {

    @State var eyesOpened: Bool = false

    var body: some View {         


        
2条回答
  •  [愿得一人]
    2021-01-03 22:45

    There is something called the notification center that you could use. In short, it is a way that views can communicate between each other without actually modifying anything in each other.

    How it works is view A sends a notification to a central hub from which view B hears said notification. When view B hears the notification it activates, and calls a function defined by the user.

    For a more detailed explanation, you can refer to: https://learnappmaking.com/notification-center-how-to-swift/

提交回复
热议问题