swiftui对成员'navigationBarTitle'的模糊引用

问题描述 投票:0回答:1

我正在通过导航视图和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)
    }
}
ios xcode alamofire swiftui
1个回答
1
投票

这只是一个随机的不同错误。使用SwiftUI时,XCode多次显示奇怪的错误消息,这些错误消息与真正的问题毫无关系。

您的实际问题是,您需要在视图层次结构的最高级(因此在.navigationBarHidden上)具有.navigationBarTitleVStack调用。因此,您需要通过以下方式更改此部分:

    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而获得的所有错误,则将编译您的代码。

© www.soinside.com 2019 - 2024. All rights reserved.