【问题标题】:self.presentationMode.wrappedValue.dismiss() not working in SwiftUIself.presentationMode.wrappedValue.dismiss() 在 SwiftUI 中不起作用
【发布时间】:2021-07-06 07:28:44
【问题描述】:

这是登录页面的代码,使用 navigation-link 从 tabview 导航 在这里我想在得到 api 响应后弹回来,

另外,如果有任何方式使用navigationLink弹出相同的链接推送,请告诉我。

提前致谢!!

struct LoginBody: View {
        @ObservedObject var viewModel = LoginViewModel()
        @State private var email: String = ""
        @State private var password: String = ""
    
    @Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>


var loginButton: some View { 
    Button(action: {
        DispatchQueue.main.async {
            print("Email:- ", email)
            print("Password:- ", password)
            
            viewModel.loginAPI(email: email, password: password)
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 8, execute: {
                print("presentationMode dismiss")
                self.presentationMode.wrappedValue.dismiss()
            })
        }
    }) {
        HStack(alignment: .center) {
            Spacer()
            Text("Login")
                .font(.system(size: 20).bold())
                .frame(alignment: .center)
            Spacer()
        }
    }
    .padding(EdgeInsets(top: 15, leading: 20, bottom: 15, trailing: 20))
    .background(Color(#colorLiteral(red: 0.3340760444, green: 0.764342023, blue: 0.8197288968, alpha: 1)))
    .foregroundColor(Color.white)
    .cornerRadius(5)
    .shadow(color: Color.init(red: 0.0, green: 0.0, blue: 0.0, opacity: 0.4), radius: 2, x: 0, y: 2)
    .frame(height: 40, alignment: .center)
}

}

【问题讨论】:

    标签: swift swiftui navigation swiftui-navigationlink swiftui-navigationview


    【解决方案1】:

    不知道如何编译,但将“loginButton”重命名为“body”。

    【讨论】:

    • 我可以知道重命名对我当前的问题有什么影响吗?
    • "body" 是 View 协议的必需计算属性。看看这个文档:developer.apple.com/documentation/swiftui/view 它是否适用于“body”?
    • 这是上面的代码 var body: some View { ZStack(alignment: .top) { self.backgroundImage VStack { self.loginButton } } }
    • @Vipin Saini,我不知道你在说什么。我已经包含了我用于测试的代码。这对你有用吗?
    • 能否请您直接联系以寻求帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2020-11-01
    • 2020-09-26
    • 2022-11-05
    • 2023-02-05
    • 2021-02-04
    • 2021-02-18
    相关资源
    最近更新 更多