SwiftUI:我无法在NavigationLink的屏幕中制作ZStack视图



这是我的NavigationLink DetailView,

import SwiftUI
struct AddChildrenView: View {
@State var word : String = ""
@State var meaning : String = ""
@State var isShowAlert : Bool = false
@State var isClicked : Bool = false
@EnvironmentObject var itemModel : ItemModel
var item : Item
var body: some View {
ZStack {
if item.children.count == 0 {
NochildrenView()
}
List {
ForEach(item.children) { child in
WordListRowView(children: child)
.onTapGesture {
withAnimation(.linear(duration: 0.3)) {
itemModel.favoriteChildren(item: item, children: child)
}
}
}
.onDelete { indexSet in
itemModel.deleteChildren1(item: item, indexSet: indexSet)
}
}
.id(UUID())
}

.navigationBarTitle("(item.group!)'s Words")
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(trailing:

HStack {
Button(action: {
addChildren()
}, label: {
Image(systemName: "plus")
})

Button(action: {
itemModel.shuffleChildren(item: item)
}, label: {
Image(systemName: "shuffle")
})

NavigationLink(destination: FlashCardView(item: item)) {
Image(systemName: "play.rectangle")
}
.disabled(item.children.isEmpty)
})
}

}

extension AddChildrenView {
func addChildren() {
let alert = UIAlertController(title: "Saving word", message: "Type word and meaning 😀", preferredStyle: .alert)

alert.addTextField { word in
word.placeholder = "Type a word"
}

alert.addTextField { meaning in
meaning.placeholder = "a meaning of word"
}

let addfolderAction = UIAlertAction(title: "Add", style: .default, handler: {
(_) in
self.word = alert.textFields![0].text!
self.meaning = alert.textFields![1].text!
itemModel.addNewChildren(item: item, word: word, meaning: meaning)
})

let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
(_) in
})

alert.addAction(addfolderAction)
alert.addAction(cancelAction)

UIApplication.shared.windows.first?.rootViewController?.present(alert, animated: true, completion: nil)
}

}

这是带有NavigationLink的MotherView,

import SwiftUI
struct HomeView: View {
@State var folderName : String = ""
@EnvironmentObject var itemModel : ItemModel
var body: some View {
NavigationView{
ZStack {
if itemModel.items.count == 0 {
NoItemView()
}
List{
ForEach(itemModel.items) {item in
if let group = item.group {
NavigationLink(destination: {
AddChildrenView(item : item)
}, label: {
HStack{
Image(systemName: "folder")
.foregroundColor(.yellow)
Text(group)
.font(.headline)
.lineLimit(1)
}
})
}
}
.onMove(perform: itemModel.moveItem)
.onDelete(perform: itemModel.deleteItem)
}
.listStyle(.plain)
}

.navigationViewStyle(StackNavigationViewStyle())
.navigationTitle("SelfDic 📒")
.navigationBarItems(trailing:
Button(action: {
addFolderView()
}, label: {
Image(systemName: "folder.badge.plus")
}))
.navigationBarItems(leading: EditButton())
}
}
}
extension HomeView {
func addFolderView() {
let alert = UIAlertController(title: "Saving folder", message: "Type a name of folder 🙂", preferredStyle: .alert)

alert.addTextField { name in
name.placeholder = "folder's name"
}

let addfolderAction = UIAlertAction(title: "Add", style: .default, handler: {
(_) in
self.folderName = alert.textFields![0].text!
itemModel.addNewFolder(text: folderName)
})

let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
(_) in
})
alert.addAction(addfolderAction)
alert.addAction(cancelAction)


UIApplication.shared.windows.first?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}

**我想在我的链接视图中使用Zstack

逻辑是,如果没有item.children,我将显示NoChildrenView((。

但是,它不起作用。我该如何处理这个问题**我希望我能解决这个问题!感谢所有资深开发者!!

我没有完全检查所有代码,但在AddChildrenView中可能缺少一个else。就像你现在拥有的一样,空列表将始终显示在你的NochildrenView()上方

if item.children.count == 0 {
NochildrenView()
} else {   // here
...

最新更新