swiftui 对成员的不明确引用 'navigationBarTitle'
swiftui Ambiguous reference to member 'navigationBarTitle'
我正在研究 swift ui 使用导航视图和 alamofire。
没有错误。
但是修改DetailView的代码就报错了
行.navigationBarHidden(showCancelButton)
--> '(@lvalue Bool) -> some View' 不能转换为 '(Bool) -> some View'
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{[=11=].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: [=11=]) }
}
}
}
}
}
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: [=10=]) }
}
}
}
}
.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 而产生的所有错误,您的代码将会编译。
我正在研究 swift ui 使用导航视图和 alamofire。 没有错误。 但是修改DetailView的代码就报错了
行.navigationBarHidden(showCancelButton) --> '(@lvalue Bool) -> some View' 不能转换为 '(Bool) -> some View'
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{[=11=].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: [=11=]) }
}
}
}
}
}
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: [=10=]) }
}
}
}
}
.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 而产生的所有错误,您的代码将会编译。