简体   繁体   English

SwiftUI,使用 actionSheet 时出现奇怪的 NavigationLink 行为

[英]SwiftUI, weird NavigationLink behavior when working with actionSheet

I want to detect if the user meets the prerequisite first before I let him/her in. If the prerequisite is not met, the app will pop an actionSheet and show the user some ways to unlock the feature.我想在让他/她进入之前先检测用户是否满足先决条件。如果不满足先决条件,应用程序将弹出一个 actionSheet 并向用户展示一些解锁该功能的方法。 It works perfectly fine when I tap on the text.当我点击文本时,它工作得很好。 But when I tap on the blank place on the list.但是当我点击列表中的空白处时。 It just skip the Binding.它只是跳过绑定。 And the weird thing is that in my actually project, the Binding becomes "true" even if I only set it to false.奇怪的是,在我的实际项目中,绑定变为“真”,即使我只将它设置为假。 Here's the question.这是问题。 Am I using the correct approach or did I miss anything?我是在使用正确的方法还是错过了什么? Or is this a bug?或者这是一个错误? Thank you.谢谢你。

struct ContentView: View {
    @State var linkOne = false
    @State var linkTwo = false
    @State var linkThree = false
    @State var actionOne = false
    @State var actionTwo = false
    @State var actionThree = false
    var body: some View {
        NavigationView{
            List{
                        NavigationLink("Destination View One", destination: DestOneView(), isActive: self.$linkOne)
                            .actionSheet(isPresented: self.$actionOne) { () -> ActionSheet in
                                ActionSheet(title: Text("Hello"), message:Text("This is weird"), buttons: [ActionSheet.Button.cancel()])
                        }.onTapGesture {
                            self.actionOne = true
//                            self.linkOne = true
                        }
                        NavigationLink("Destination View Two", destination: DestTwoView(), isActive: self.$linkTwo)
                        .actionSheet(isPresented: self.$actionTwo) { () -> ActionSheet in
                        ActionSheet(title: Text("Hello"), message:Text("This is weird"), buttons: [ActionSheet.Button.cancel()])
                        }
                        .onTapGesture {
                            self.actionTwo = true
//                            self.linkTwo = true
                        }
                        NavigationLink("Destination View Three", destination: DestThreeView(), isActive: self.$linkThree)
                        .actionSheet(isPresented: self.$actionThree) { () -> ActionSheet in
                        ActionSheet(title: Text("Hello"), message:Text("This is weird"), buttons: [ActionSheet.Button.cancel()])
                        }
                        .onTapGesture {
                            self.actionThree = true
//                            self.linkThree = true
                        }
                    }
        }

    }
}

Three other views.其他三种观点。

struct DestOneView: View {
    var body: some View {
        Text("First View")
    }
}

struct DestTwoView: View {
    var body: some View {
        Text("Second View")
    }
}

struct DestThreeView: View {
    var body: some View {
        Text("Third View")
    }
}

Generally overriding gestures does not work well within the List .通常,覆盖手势在List中效果不佳。 One of the solutions can be to use a Button to present a NavigationLink :解决方案之一是使用Button来呈现NavigationLink

import SwiftUI

struct ContentView: View {
    @State private var linkOne = false
    ...

    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: SomeView(), isActive: $linkOne) {
                    EmptyView() 
                }

                Button(action: {
                    // here you can perform actions
                    self.linkOne = true
                }, label: {
                    Text("Some text!")
                })

                Spacer()
            }
        }
    }
}

When I came back and tested my the code.当我回来测试我的代码时。 The solution didn't really work.解决方案并没有真正奏效。 May be because of the List's bug.可能是因为列表的错误。 People on another post said that the List in the new view only show once if the sheet is inside the List.另一个帖子上的人说,如果工作表在列表中,新视图中的列表只会显示一次。 So I only got an empty List in the new view when I tap the button in Xcode Version 11.5.因此,当我点击 Xcode 版本 11.5 中的按钮时,我在新视图中只得到一个空列表。 For some reasion, if I use NavigationView, all contents are shrunk into the middle of the view instead of aligning to the top.出于某种原因,如果我使用 NavigationView,所有内容都会缩小到视图的中间,而不是与顶部对齐。

My work around is to set the Binding in.onAppear.我的解决方法是在.onAppear 中设置绑定。 It pops the actionSheet when the view loads.它会在视图加载时弹出 actionSheet。 And then use the presentationMode method to return to the previous view.然后使用presentationMode方法返回上一个视图。

@Environment(\.presentationMode) var presentationMode
.
.
.
ActionSheet.Button.default(Text("Dismiss"), action: {
          self.presentationMode.wrappedValue.dismiss()}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM