Ios swiftui对成员的引用不明确';导航巴蒂尔';
我正在研究带有导航视图和alamofire的swift ui。 没有错误。 然而,当我修改关于DetailView的代码时,出现了一个错误 Inline.navigationBarHidden(显示取消按钮) -->“(@lvalue Bool)->某些视图”不能转换为“(Bool)->某些视图” 行内组{In DetailView -->对成员“navigationBarTitle”的引用不明确 请帮助:(Ios swiftui对成员的引用不明确';导航巴蒂尔';,ios,xcode,alamofire,swiftui,Ios,Xcode,Alamofire,Swiftui,我正在研究带有导航视图和alamofire的swift ui。 没有错误。 然而,当我修改关于DetailView的代码时,出现了一个错误 Inline.navigationBarHidden(显示取消按钮) -->“(@lvalue Bool)->某些视图”不能转换为“(Bool)->某些视图” 行内组{In DetailView -->对成员“navigationBarTitle”的引用不明确 请帮助:( 这只是一个随机的不同错误。当使用SwiftUI时,XCode会多次显示奇怪的错误消息,
这只是一个随机的不同错误。当使用SwiftUI时,XCode会多次显示奇怪的错误消息,而这些消息与真正的问题没有任何联系 您真正的问题是需要在视图层次结构的最高层调用
.navigationBarHidden
和.navigationBarTitle
(在VStack
上也是如此)。因此,您需要以以下方式更改此部分:
VStack {
[...]
if showCancelButton {
Button("Cancel") {
UIApplication.shared.endEditing(true) // this must be placed before the other commands here
self.searchText = ""
self.showCancelButton = false
}
.foregroundColor(Color(.systemBlue))
}
}
.padding(.horizontal)
//.navigationBarHidden(showCancelButton)
List {
ForEach(newsList, id: \.self) { news in
NavigationLink(
destination: DetailView(selectedNews: news)
) {
Text(news.title)
}
}.onDelete { indices in
indices.forEach { self.newsList.remove(at: $0) }
}
}
}
}
.navigationBarHidden(showCancelButton)
.navigationBarTitle(Text(selectedNews.title))
[...]
此时唯一的错误是,selectedNews
未在MasterView结构中声明。因此您只需将其移动到那里:
struct MasterView: View {
@State private var searchText = ""
@State private var showCancelButton: Bool = false
var selectedNews: News //<-- move here!
@Binding var newsList: [News]
var body: some View {
VStack {
// Search view
HStack {
HStack {
[...]
struct主视图:视图{
@国家私有var searchText=“”
@国家私有var showCancelButton:Bool=false
var selectedNews:News//
struct MasterView: View {
@State private var searchText = ""
@State private var showCancelButton: Bool = false
var selectedNews: News //<-- move here!
@Binding var newsList: [News]
var body: some View {
VStack {
// Search view
HStack {
HStack {
[...]