swiftui 对成员“navigationBarTitle”的模糊引用

Posted

技术标签:

【中文标题】swiftui 对成员“navigationBarTitle”的模糊引用【英文标题】:swiftui Ambiguous reference to member 'navigationBarTitle' 【发布时间】:2020-01-18 15:37:10 【问题描述】:

我正在学习带有导航视图和 alamofire 的 swift ui。 没有错误。 但是,当我修改有关DetailView的代码时出现错误。

行内 .navigationBarHidden(showCancelButton) --> '(@lvalue Bool) -> some View' 不能转换为 '(Bool) -> some View'

In line Group 在 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)
    

【问题讨论】:

【参考方案1】:

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

您真正的问题是您需要在 View Hierarchy 的最上层调用 .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 
                    [...]

如果你纠正了修改MasterView和DetailView的init-s的所有错误,你的代码将会编译。

【讨论】:

以上是关于swiftui 对成员“navigationBarTitle”的模糊引用的主要内容,如果未能解决你的问题,请参考以下文章

当视图在 SwiftUI 中消失时如何隐藏 NavigationBar?

如何在 SwiftUI 中隐藏额外的 NavigationBar

在 NavigationLink 之后,在 NavigationView 内、TabView 内显示 NavigationBar。 SwiftUI

SwiftUI NavigationBar 后退按钮在使用自定义字体时抱怨图标缩放

SwiftUI-NavigationLink中的NavigationBar在视图中迅速显示然后消失

swiftui 对成员“navigationBarTitle”的模糊引用