在SwiftUI中将NavigationButton与服务器请求一起使用 [英] Use NavigationButton with a server request in SwiftUI

查看:178
本文介绍了在SwiftUI中将NavigationButton与服务器请求一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进入下一个视图之前,如何使NavigationButton等待服务器响应?

How can I make a NavigationButton to wait for the server response before going to the next view ?

我已经尝试过类似的事情

I've tried something like this

NavigationButton(destination: LogonView(),  onTrigger: { () -> Bool in
                    return self.viewModel.responseReceived
                }) {
                    Text("OK")
                    }.tapAction {
                        self.viewModel.fetch(companyID: &self.companyID)
                }

但从未调用tapAction.

我使用Button使其工作:

Button(action: {
        self.viewModel.fetch(companyID: &self.companyID)
    }) {
        Text("OK")
    }.presentation(viewModel.shouldPresentModal ? Modal(LogonView() : nil)

    // in ViewModel
    var shouldPresentModal = false { // set to true when data are received from server
            didSet {
                didChange.send(())
            }
        }

但是我需要在导航中显示下一个视图,而不是模态显示

but I need to show the next view in a navigation, not modally

谢谢!

推荐答案

Sorin,至少在我的理解中,SwiftUI仅是为表示层设计的,不应替换您的模型.而且它与UIKit不同,它是反应性的",因此,设计使视图执行类似模型的动作非常困难.

Sorin, at least in my understanding SwiftUI is designed only for the presentation layer, it's not supposed to replace your model. And it's "reactive", unlike UIKit, so making the view perform model-like actions is, by design, very hard.

我将执行以下任务:

class LoginModel : BindableObject {

    var didChange = PassthroughSubject<LoginModel, Never>()

    private(set) var username: String? {
        didSet {
            didChange.send(self)
        }
    }

    func load() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
            self.username = "Sorin"
        }
    }
}

这是封装我们的登录代码的模型对象.这里的异步操作是通过一个简单的延迟来模拟的.

This is the model object encapsulating our login code. The async operation is here simulated by a simple delay.

然后,视图:

public struct LoginScreen: View {

    @ObjectBinding var loginObject = LoginModel()

    public var body: some View {
        Group {
            if login.username == nil {
                Text("Trying to login, please wait...")
            } else {
                Text("Successful login, the username is \(loginObject.username!)")
            }
        }.onAppear {
            self.loginObject.load()
        }
    }
}

有更好的方法与模型对象链接",但显然,我们在这里只是看一个简单的例子.

There are better ways of "linking" with the model object, but we're looking here only at a bare-bone example, obviously.

您的NavigationButton将仅链接到LoginScreen,而没有任何其他代码或触发器. 屏幕最初将显示Trying to login, please wait...,并在5秒钟后更改为Successful login, the username is Sorin.显然,您可能会大吃一惊,并用任何您想要的内容替换我的文字.

Your NavigationButton will only link to the LoginScreen, without any other code or trigger. The screen will initially display Trying to login, please wait... and after 5 sec will change to Successful login, the username is Sorin. Obviously you could go wild and replace my text here with anything you may want.

这篇关于在SwiftUI中将NavigationButton与服务器请求一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆