因此,当我单击其他两个选项卡栏项目时,它们会起作用,但是,当我单击第三个选项卡时,它会崩溃。我的导航已连接到所有内容
//编辑:如果您想为标签栏项目提供解决方案,我们需要您的TabBar代码,尤其是指示错误所在的地方。似乎您强行打开了一个空的可选内容。
[可能只是和我一样,但在当时的SwiftUI中,使用Tab栏通常很麻烦。我使用的是自定义标签栏,因此只是一个自定义视图,然后将其添加到其他视图的底部,我也可以使用ViewController对其进行控制。
示例:
CustomNavigation.swift:
struct CustomNavigation : View {
@ObservedObject var viewRouter: ViewRouter
let lightGreyColor = Color(red: 102.0/255.0, green: 101.0/255.0, blue: 110.0/255.0, opacity: 0.6)
var body: some View {
HStack {
Group {
Button (action: {
self.viewRouter.currentView = "home"
}) {
HStack(spacing: 15) {
Image(systemName: "house")
if self.viewRouter.currentView == "home" {
Text("Home")
}
}
.padding(.vertical, 10)
.padding(.horizontal)
}
.foregroundColor(.white)
.background(Color.blue.opacity(self.viewRouter.currentView == "home" ? 1 : 0))
.clipShape(RoundedRectangle(cornerRadius: 12))
Spacer(minLength: 0)
Button (action: {
self.viewRouter.currentView = "search"
}) {
HStack(spacing: 15) {
Image(systemName: "magnifyingglass")
if self.viewRouter.currentView == "search" {
Text("Search")
}
}
.padding(.vertical, 10)
.padding(.horizontal)
}
.foregroundColor(.white)
.background(Color.blue.opacity(self.viewRouter.currentView == "search" ? 1 : 0))
.clipShape(RoundedRectangle(cornerRadius: 12))
Spacer(minLength: 0)
Button (action: {
self.viewRouter.currentView = "reserved"
}) {
HStack(spacing: 15) {
Image(systemName: "cart")
if self.viewRouter.currentView == "reserved" {
Text("Reservierungen")
}
}
.padding(.vertical, 10)
.padding(.horizontal)
}
.foregroundColor(.white)
.background(Color.blue.opacity(self.viewRouter.currentView == "reserved" ? 1 : 0))
.clipShape(RoundedRectangle(cornerRadius: 12))
Spacer(minLength: 0)
Button (action: {
self.viewRouter.currentView = "settings"
}) {
HStack(spacing: 15) {
Image(systemName: "person")
if self.viewRouter.currentView == "settings" {
Text("Account")
}
}
.padding(.vertical, 10)
.padding(.horizontal)
}
.foregroundColor(.white)
.background(Color.blue.opacity(self.viewRouter.currentView == "settings" ? 1 : 0))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
.padding(.vertical)
.padding(.horizontal, 25)
.padding(.bottom, UIApplication.shared.windows.first?.safeAreaInsets.bottom)
.background(lightGreyColor)
// .cornerRadius(5)
.animation(.default)
}
}
ViewRouter.swift:(EnvironmentObject!)
import Foundation
import SwiftUI
import Combine
class ViewRouter: ObservableObject {
@Published var currentView = "home"
}
实施示例:
struct HomeView: View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
return Group {
if self.viewRouter.currentView == "home" {
HomeFormView()
} else if self.viewRouter.currentView == "settings" {
SettingsView()
}
else if self.viewRouter.currentView == "search" {
LoungeList()
}
else if self.viewRouter.currentView == "reserved" {
ReservedView()
}
else if self.viewRouter.currentView == "order" {
OrderView()
}
else {
// .....
}
}
}
}
也许这也可以解决您的问题,我一点都没有问题。因为TabBarView对我来说太麻烦了。您必须在SceneDelegate中将ViewRouter设置为environmentObject:
window.rootViewController = UIHostingController(rootView: contentView.environmentObject(ViewRouter()))
希望这会有所帮助!