堆栈中的伟大人物如何溢出。 我在 swift UI 中遇到问题
我在 NavigationView 中有几个视图 像那样:
tab1:
ShowEventsView
tab2:
View1->View2->View3->View4
我希望 tab2 创建一个“体育赛事”并有一个 feed 来显示在 tab1 中为用户创建的事件,所以问题是当我刚刚创建事件时自动将 tab3 更改为 tab1。
不确定,推荐使用navigationView还是navigationStack?
enum Tab {
case home
case calendar
case add
case group
case profile
}
var body: some View {
TabView(selection: $selection){
Home().environmentObject(ReadEvents())
.tabItem {
Label("Home", systemImage: "house")
}
.tag(Tab.home)
CalendarView()
.tabItem {
Label("Calendar", systemImage: "calendar")
}
.tag(Tab.calendar)
Add().environmentObject(ModelData()).environmentObject(MapItemViewModel()).environmentObject(SportViewModel())
.tabItem{
Label("Add",systemImage: "plus.app")
}
.tag(Tab.add)
Group()
.tabItem{
Label("Group",systemImage: "person.and.person.fill")
}
Profile()
.tabItem{
Label("Profile",systemImage: "person")
}
}.accentColor(accentColor)
}
import SwiftUI
import MapKit
struct CreateEvent: View {
@EnvironmentObject var modelData: ModelData
@EnvironmentObject var networkMonitor: NetworkMonitor
@State private var showNetworkAlert = false
@State private var rootIsAactive:Bool = false
private var isValid:Bool{
!modelData.eventName.isEmpty && modelData.eventName.count>3
}
var body: some View {
if networkMonitor.isConnectedFireBase && networkMonitor.isConnected{
NavigationView {
NavigationStack{
Text("Name Event")
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal)
TextField("Micro Tournament",text: $modelData.eventName)
.padding(.horizontal)
.textFieldStyle(.roundedBorder)
Text("Where does the event take place: ")
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal)
MapItem()
}
.navigationTitle("Create Event")
.navigationBarItems(trailing:
NavigationLink(
destination: SelectSport(rootIsActive: $rootIsAactive),
isActive: $rootIsAactive
){
Text("Next")
}
.isDetailLink(false)
.disabled(!isValid)
)
}.onChange(of: networkMonitor.isConnected) { connection in
showNetworkAlert = connection == false
}
.alert(
"Network connection seems to be offline.",
isPresented: $showNetworkAlert
) {}
}else{
NotConnection()
}
}
}
其他视图具有非常相似的结构,最后一个视图有一个上传事件的按钮:
Button(action: {
sendNotification()
CreateEvent()
showingAlert = true
}, label: {
Text("Create")
.frame(maxWidth: .infinity)
})
.alert(isPresented: $showingAlert){
Alert(
title: Text("Event Created"),
message: Text("Wear sunscreen!"),
dismissButton: .default(Text("Got it!"),action: {
showingHome = true
})
)
}