且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在回调,SwiftUI中以编程方式推送View

更新时间:2023-12-04 21:11:16

我找到了答案.如果要在回调中显示其他视图,则应

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

1)创建状态@State var pushActive = false

2)当ViewModel通知登录成功时,将pushActive设置为true

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

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

3)创建隐藏的NavigationLink并绑定到该状态

3) Create hidden NavigationLink and bind to that state

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