我正在用导航视图和alamofire研究swift ui。
没有错误。
但是,当我修改有关DetailView的代码时出现错误。
在.navigationBarHidden(showCancelButton)行中
->'(@lvalue Bool)->一些视图'不能转换为'(Bool)->一些视图'
串联组{在DetailView中
->对成员'navigationBarTitle'的引用不明确
请帮忙 :(
import SwiftUI
import Alamofire
struct News:Hashable {
var title :String?
var reporter : String?
var press : String?
var link : String?
var originalLink : String?
}
extension UIApplication {
func endEditing(_ force: Bool) {
self.windows
.filter{$0.isKeyWindow}
.first?
.endEditing(force)
}
}
struct ContentView: View {
@State var selection = 0
var body: some View {
TabView (selection: $selection){
FeedView()
.tabItem {
HStack {
Image(systemName: "list.dash")
Text("Feed")
}
}
.tag(0)
SearchView()
.tabItem {
HStack {
Image(systemName: "magnifyingglass.circle")
Text("Search")
}
}
.tag(1)
}
}
}
struct FeedView: View {
var body: some View {
Text("Feed")
}
}
struct SearchView: View {
@State private var newsList = [News]()
var body: some View {
NavigationView {
MasterView(newsList: $newsList)
.navigationBarTitle(Text("Search News"))
.navigationBarItems(
leading: EditButton(),
trailing: Button(
action: {
withAnimation { }
}
) {
Image(systemName: "plus")
}
)
DetailView(selectedNews: News())
}.navigationViewStyle(DoubleColumnNavigationViewStyle())
}
}
struct MasterView: View {
@State private var searchText = ""
@State private var showCancelButton: Bool = false
@Binding var newsList: [News]
var body: some View {
VStack {
// Search view
HStack {
HStack {
Image(systemName: "magnifyingglass")
TextField("search", text: $searchText, onEditingChanged: { isEditing in
self.showCancelButton = true
}, onCommit: {
self.showCancelButton = false
let apiUrl = "https://openapi.naver.com/v1/search/news.json?query="
let search = self.searchText.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!
Alamofire.request(apiUrl+search, method: .get, headers: [ "X-Naver-Client-Id": "*********", "X-Naver-Client-Secret":"**********"])
.responseJSON { response in
let newsList = (response.result.value as! [String:Any])["items"]!
self.newsList = [News]()
for news in (newsList as! [[String:String]]) {
self.newsList.append(News(title: news["title"], link: news["link"], originalLink: news["originallink"]))
}
}
}).foregroundColor(.primary)
Button(action: {
//self.searchText = ""
}) {
Image(systemName: "xmark.circle.fill").opacity(searchText == "" ? 0 : 1)
}
}
.padding(EdgeInsets(top: 8, leading: 6, bottom: 8, trailing: 6))
.foregroundColor(.secondary)
.background(Color(.secondarySystemBackground))
.cornerRadius(10.0)
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) }
}
}
}
}
}
struct DetailView: View {
var selectedNews: News
var body: some View {
Group{
Text("Hello")
}.navigationBarTitle(Text(selectedNews.title))
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(selection: 1)
}
}
最佳答案
那只是一个随机的不同错误。 XCode在使用SwiftUI时会多次显示奇怪的错误消息,这些错误消息与真正的问题毫无关系。
真正的问题是,您需要在视图层次结构的最高级别(因此在.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))
[...]
此时,您唯一的错误是在MasterView结构中未声明
selectedNews
。所以你只需要把它移到那里: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 {
[...]
然后,如果您纠正了通过修改MasterView和DetailView的
init
-s所获得的所有错误,则将编译您的代码。关于ios - swiftui对成员'navigationBarTitle'的模糊引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59802070/