Push View programmatically in callback, SwiftUI

后端 未结 5 1620
长发绾君心
长发绾君心 2020-12-17 09:25

It seems to me that Apple is encouraging us to give up using UIViewController in SwiftUI, but without using view controlelrs, I feel a little bit powerless. Wha

5条回答
  •  暖寄归人
    2020-12-17 10:04

    I've found the answer. If you want to show another view on callback you should

    1) Create state @State var pushActive = false

    2) When ViewModel notifies that login is successful set pushActive to true

      func handleSuccessfullLogin() {
        self.pushActive = true
        print("handleSuccessfullLogin")
      }
    

    3) Create hidden NavigationLink and bind to that state

      NavigationLink(destination: ProfileView(viewModel: ProfileViewModelImpl()), isActive: self.pushActive) {
        Text("")
      }.hidden()
    

提交回复
热议问题