导航栏返回按钮在显示警报后消失。

huangapple go评论62阅读模式
英文:

Navigation Bar back button disappears after showing an alert

问题

我有一个简单的视图:

struct AccountView: View {
@State private var showingLogoutAlert = false
var body: some View {
Button("Log out!") {
showingLogoutAlert = true
}
.alert(isPresented: $showingLogoutAlert, content: {
Alert(title: Text("Log out of Flow?"),
primaryButton: .cancel(),
secondaryButton: .destructive(Text("Log out"), action: {
//logOut()
}))
})

}

}


当你点击按钮时,会显示一个警告。问题是,警告消失后,返回按钮也消失了!我不知道为什么会发生这种情况。请看下面的gif图示。

[![enter image description here](https://i.stack.imgur.com/MxAWC.gif)](https://i.stack.imgur.com/MxAWC.gif)

我已经尝试将问题减少到最少的代码。在一个单独的新应用程序中复制了它。仍然存在相同的问题。
英文:

I have a simple view:

struct AccountView: View {
    @State private var showingLogoutAlert = false
    var body: some View {
        Button("Log out!") {
            showingLogoutAlert = true
        }
        .alert(isPresented: $showingLogoutAlert, content: {
            Alert(title: Text("Log out of Flow?"),
                  primaryButton: .cancel(),
                  secondaryButton: .destructive(Text("Log out"), action: {
                //logOut()
            }))
        })
        
    }
}

When you tap the button, it will show an alert. The problem is, after the alert dismisses, the back button also disappears! I have no idea why this is happening. See the gif below.

导航栏返回按钮在显示警报后消失。

I've tried to reduce the problem down to the bare minimum code. Replicated it on a separate, new app. Still the same issue.

答案1

得分: 0

这是我在我的测试中使用的工作代码,在MacOS 13.2、Xcode 14.2上进行了测试,已在真实的iOS 16.3设备(而非预览)和macCatalyst上测试。

struct ContentView: View {
    var body: some View {
        NavigationStack {
            NavigationLink(destination: AccountView()) {
                Text("AccountView")
            }
        }
    }
}

struct AccountView: View {
    @State private var showingLogoutAlert = false
    var body: some View {
        Button("Log out!") {
            showingLogoutAlert = true
        }
        .alert(isPresented: $showingLogoutAlert, content: {
            Alert(title: Text("Log out of Flow?"),
                  primaryButton: .cancel(),
                  secondaryButton: .destructive(Text("Log out"), action: {
                //logOut()
            }))
        }
    }
}

希望这对你有帮助。

英文:

this is the working code I used in my test, on MacOS 13.2, Xcode 14.2, tested on real ios 16.3 devices (not Previews), and macCatalyst.

struct ContentView: View {
    var body: some View {
        NavigationStack {
            NavigationLink(destination: AccountView()) {
                Text("AccountView")
            }
        }
    }
}

struct AccountView: View {
    @State private var showingLogoutAlert = false
    var body: some View {
        Button("Log out!") {
            showingLogoutAlert = true
        }
        .alert(isPresented: $showingLogoutAlert, content: {
            Alert(title: Text("Log out of Flow?"),
                  primaryButton: .cancel(),
                  secondaryButton: .destructive(Text("Log out"), action: {
                //logOut()
            }))
        })
    }
}

huangapple
  • 本文由 发表于 2023年2月8日 15:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定