SwiftUI 中的异步下一屏幕演示 [英] Async Next Screen Presentation in SwiftUI

查看:40
本文介绍了SwiftUI 中的异步下一屏幕演示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击我的应用中的注册按钮时注册用户.当注册完成并在服务器端成功创建用户时,我想显示下一个屏幕,然后.

i want to signup users when they click the signup button in my app. When the signup is complete and successfully created a user on the server side I want to present the next screen and only then.

在正常情况下,我有一个 PresentationButton 并设置目的地,当有人单击按钮时,下一个屏幕会直接显示,但现在它是异步的.

In normal way I have a PresentationButton and set the destination and when somebody clicked the button the next screen is presented directly, but now it's async.

如何处理?

目前我有这个 PresentationButton:

Currently I have this PresentationButton:

PresentationButton(
                Text(isSignIn ? "SignIn" : "Next").font(.headline).bold()
                    .frame(width: 100)
                    .padding(10)
                    .foregroundColor(.white)
                    .background(Color.blue)
                    .cornerRadius(20)
                , destination: HomeScreen()
            )

这是 Uro Arangino 的建议:

That's with the suggestion by Uro Arangino:

struct PasswordView : View {
    @State private var password = ""
    @State private var showNextScreen = false

    var loginMode: LoginType

    @ObjectBinding var signupManager = SignUpManager()

    var body: some View {
        VStack {
//            if self.signupManager.showModal { self.showNextScreen.toggle() } <- Can't do this

            TwitterNavigationView(backBtnOn: false)
            middleView()
            Spacer()
            bottomView()
        }
    }

    func bottomView() -> some View {
        return VStack(alignment: .trailing) {
            Divider()
            BindedPresentationButton(
                showModal: $showNextScreen,
                label: getCustomText((Text("Registrieren"))),
                destination: HomeTabView(),
                onTrigger: {
                    let user = User(name: "rezo", username: "ja lol ey", profileDescription: "YAS", email: "dbjdb@dedde.de", telephoneNumber: nil, profileImage: UIImage(named: "twitter-logo")!, bannerImage: UIImage(systemName: "star")!)
                    self.signupManager.signIn(forUser: user, password: "ultraSecure", loginMode: .email)
//                UserDefaults.standard.set(true, forKey: "loggedIn")
            })
        }.padding([.leading, .trailing]).padding(.top, 5).padding(.bottom, 10)
    }
}

我的可绑定对象类

final class SignUpManager: BindableObject {
let didChange = PassthroughSubject<SignUpManager, Never>()

var showModal: Bool = false {
    didSet {
        didChange.send(self)
    }
}

func signIn(forUser user: User, password: String, loginMode: LoginType) {
    if loginMode == .email {
        LoginService.instance.signupWithEmail(user: user, andPassword: password, completion: handleCompletion)
    } else {
        LoginService.instance.login(withPhoneNumber: user.telephoneNumber!, completion: handleCompletion)
    }
}

private func handleCompletion(_ status: Bool) {
    if status {
        showModal = true
    }
}

}

推荐答案

您可以使用带有绑定的演示按钮.

You can use a presentation button with a binding.

参见:https://stackoverflow.com/a/56547016/3716612

struct ContentView: View {
    @State var showModal = false

    var body: some View {
        BindedPresentationButton(
            showModal: $isSignIn,
            label: Text(isSignIn ? "SignIn" : "Next")
                .font(.headline)
                .bold()
                .frame(width: 100)
                .padding(10)
                .foregroundColor(.white)
                .background(Color.blue)
                .cornerRadius(20),
            destination: HomeScreen()
        )
    }
}

这篇关于SwiftUI 中的异步下一屏幕演示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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