我在将 1 个视图推送到另一个视图时遇到问题。让我解释一下层次结构。
ContentView -> 2 个选项卡,TabAView 和 TabBView 在 TabBView 内。有 1 个视图使用了 ConnectView:连接按钮在哪里。点击 Connect 按钮后,用户移动到另一个视图,称为 UserAppView。
这是更好地理解我的问题的代码。
内容视图.swift
struct ContentView: View {
enum AppPage: Int {
case TabA=0, TabB=1
}
@StateObject var settings = Settings()
@ObservedObject var userViewModel: UserViewModel
@ObservedObject var userAppViewModel: UserAppViewModel
var body: some View {
NavigationView {
TabView(selection: $settings.tabItem) {
TabAView(userViewModel: userViewModel, userAppViewModel: userAppViewModel)
.tabItem {
Text("TabA")
}
.tag(AppPage.TabA)
TabBView(userViewModel: userViewModel, userAppViewModel: userAppViewModel)
.tabItem {
Text("Apps")
}
.tag(AppPage.TabB)
}
.accentColor(.white)
.edgesIgnoringSafeArea(.top)
.onAppear(perform: {
settings.tabItem = .TabA
})
.navigationBarTitleDisplayMode(.inline)
}
.environmentObject(settings)
}
}
这是 TabAView:
struct TabAView: View {
@ObservedObject var userViewModel: UserViewModel
@ObservedObject var userAppViewModel: UserAppViewModel
@EnvironmentObject var settings: Settings
init(userViewModel: UserViewModel, userAppViewModel: UserAppViewModel) {
self.userViewModel = userViewModel
self.userAppViewModel = userAppViewModel
}
var body: some View {
Vstack {
/// code
}
.onAppear(perform: {
/// code
})
.environmentObject(settings)
}
}
这是另一个 TabBView:
struct TabBView: View {
@ObservedObject var userViewModel: UserViewModel
@ObservedObject var userAppViewModel: UserAppViewModel
init(userViewModel: UserViewModel, userAppViewModel: UserAppViewModel) {
self.userViewModel = userViewModel
self.userAppViewModel = userAppViewModel
}
var body: some View {
VStack (spacing: 10) {
NavigationLink(destination: ConnectView(viewModel: ConnectViewModel(id: id!), userViewModel: userViewModel, userAppViewModel: userAppViewModel)) {
UserCardWidget()
}
}
}
}
TabBView 上使用了 1 个 connectView,用户将通过该 connectView 进行连接。此处使用 ConnectViewModel 来调用连接 API。
class ConnectViewModel: ObservableObject {
var id: String?
init(id: String) {
self.id = id
}
func connect(completion: @escaping () -> Void) {
APIService.shared.connectApp(id: self.id!) { connected in
DispatchQueue.main.async {
self.isConnected = connected ?? false
completion()
}
}
}
}
这是连接视图
struct ConnectView: View {
@ObservedObject var connectViewModel: ConnectViewModel
@ObservedObject var userViewModel: UserViewModel
@ObservedObject var userAppViewModel: UserAppViewModel
@State var buttonTitle = "CONNECT WITH THIS"
@State var isShowingDetailView = false
var body: some View {
VStack {
Spacer()
if let id = connectViewModel.id {
NavigationLink(destination: UserAppView(id: id, userAppViewModel: userAppViewModel, userViewModel: userViewModel), isActive: $isShowingDetailView) {
Button(buttonTitle, action: {
connectViewModel.connect {
buttonTitle = "CONNECTED"
isShowingDetailView = true
}
})
}
}
}
}
}
这是调用 API 以获取一些与用户相关的详细信息的 UserAppViewModel:
类 UserAppViewModel: ObservableObject {
var id = ""
func getdetails() {
APIService.shared.getDetails() { userDetails in
DispatchQueue.main.async {
/// code
}
}
}
}
这是 UserAppView 类
struct UserAppView: View {
@ObservedObject var userViewModel: UserViewModel
@ObservedObject var userAppViewModel: UserAppViewModel
init(id: String, userAppViewModel: UserAppViewModel, userViewModel: UserViewModel) {
self.id = id
self.userAppViewModel = userAppViewModel
self.userViewModel = userViewModel
}
var body: some View {
VStack {
Text(userAppViewModel.status)
}.onAppear(perform: {
**userAppViewModel.getDetails**(id: id)
})
}
}
每当执行此语句userAppViewModel.getDetails时,用户会来到 UserAppView,但是当收到 getDetails 的响应时,它会自动转到 ConnectView。
从机密的角度来看,我已经删除了大部分代码,但这段代码将解释原因和错误。请查看代码并帮助我。