我需要一个十分钟的人来告诉我为什么我的列表无法通过NavigationLink显示在另一个视图中。我可以看到在“ roadInfo”视图中显示的列表,但里面没有任何内容。不使用navigationLink时,将显示我的列表,没有问题。但是我需要使用navigationLink显示此列表。
在这里,当我选择“ paris-Nord”行时,我通过NavigationLink重定向到一个新视图,但是我的列表为空! (您可以在下一张图片中看到它)
我认为问题是这样的:
NavigationLink(destination: RoadInfo(depart: self.$Depart, destination:
self.$Destination), isActive: self.$hey) {
EmptyView()
}
我需要一个NavigationLink才能在我没有选择的另一个视图中显示结果。
有我的代码:
ContentView:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
StationDetail()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
StationDetail:
import SwiftUI
struct StationDetail: View {
@ObservedObject var webservice = Webservice()
@State var Depart: String = ""
@State var Destination: String = ""
@State var listStation = [Station]()
@State var predictedValues: Array<String> = []
@State var isBeingEditedDeparture: Bool = false
@State var isBeingEditedDestination: Bool = false
@State var hey: Bool = false
var body: some View {
VStack(alignment: .leading){
NavigationLink(destination: RoadInfo(depart: self.$Depart, destination: self.$Destination), isActive: self.$hey) {
EmptyView()
}
PredictingTextField(listStation: self.$listStation , predictedValues:
self.$predictedValues, Depart: self.$Depart, Destination: self.$Destination,
isBeingEditedDeparture: self.$isBeingEditedDeparture, isBeingEditedDestination:
self.$isBeingEditedDestination )
.textFieldStyle(RoundedBorderTextFieldStyle())
List() {
ForEach(self.predictedValues, id: \.self){ value in
Text(value)
.onTapGesture {
if(self.isBeingEditedDeparture == true)
{
self.Depart = value
}
else{
self.Destination = value
self.hey = true
}
}
}
}
}
}
struct StationDetail_Previews: PreviewProvider {
static var previews: some View {
StationDetail()
}
}
/// TextField capable of making predictions based on provided predictable values
struct PredictingTextField: View {
@ObservedObject var webservice = Webservice()
@Binding var Depart: String
@Binding var Destination: String
@Binding var listStation: Array<Station>
@Binding var predictedValues: Array<String>
@State var predictionInterval: Double?
//@State private var isBeingEdited: Bool = false
@Binding var isBeingEditedDeparture: Bool
@Binding var isBeingEditedDestination: Bool
init(listStation:Binding<[Station]> ,
predictedValues: Binding<Array<String>>, Depart: Binding<String>, Destination: Binding<String>,isBeingEditedDeparture: Binding<Bool>, isBeingEditedDestination: Binding<Bool>, predictionInterval: Double? = 0.1 ){
self._listStation = listStation
self._predictedValues = predictedValues
self._Depart = Depart
self._Destination = Destination
self._isBeingEditedDeparture = isBeingEditedDeparture
self._isBeingEditedDestination = isBeingEditedDestination
self.predictionInterval = predictionInterval
}
var body: some View {
VStack {
TextField("Depart", text: $Depart, onEditingChanged: { editing in self.realTimePredictionDeparture(status: editing)}, onCommit: { self.makePrediction()
})
TextField("Destination", text: $Destination, onEditingChanged: { editing in self.realTimePredictionDestination(status: editing)}, onCommit: { self.makePrediction()
})
}.padding()
}
private func realTimePredictionDeparture(status: Bool){
self.isBeingEditedDeparture = status
if status == true {
Timer.scheduledTimer(withTimeInterval: self.predictionInterval ?? 1, repeats: true) { timer in
self.makePrediction()
if self.isBeingEditedDeparture == false {
timer.invalidate()
}
}
}
}
private func realTimePredictionDestination(status: Bool) {
self.isBeingEditedDestination = status
if status == true {
Timer.scheduledTimer(withTimeInterval: self.predictionInterval ?? 1, repeats: true) { timer in
self.makePrediction()
if self.isBeingEditedDestination == false {
timer.invalidate()
}
}
}
}
private func capitalizeFirstLetter(smallString: String) -> String {
return smallString.prefix(1).capitalized + smallString.dropFirst()
}
private func makePrediction() {
self.listStation = self.webservice.getStations()
self.predictedValues = []
if !self.Depart.isEmpty && self.isBeingEditedDeparture == true {
for value in self.listStation{
if value.name.contains(self.Depart) || value.name.contains(self.capitalizeFirstLetter(smallString: self.Depart)) {
if !self.predictedValues.contains(value.name){
self.predictedValues.append(value.name)
}
}
}
}
if !self.Destination.isEmpty && self.isBeingEditedDestination == true {
for value in self.listStation{
if value.name.contains(self.Destination) || value.name.contains(self.capitalizeFirstLetter(smallString: self.Destination)) {
if !self.predictedValues.contains(value.name){
self.predictedValues.append(value.name)
}
}
}
}
}
private func makeMultiPrediction(value: String) {
for subString in self.Depart.split(separator:" ") {
if value.contains(String(subString)) || value.contains(self.capitalizeFirstLetter(smallString: String(subString)))
{
if !self.predictedValues.contains(value){
self.predictedValues.append(value)
}
}
}
for subString in self.Destination.split(separator:" ") {
if value.contains(String(subString)) || value.contains(self.capitalizeFirstLetter(smallString: String(subString)))
{
if !self.predictedValues.contains(value){
self.predictedValues.append(value)
}
}
}
}
}
}
RoadInfo:
import SwiftUI
struct RoadInfo: View {
@ObservedObject var webservice = Webservice()
@Binding var depart: String
@Binding var destination: String
var body: some View {
HStack {
List(){
ForEach(webservice.rides, id: \.id) {v in
RowList(connection: v )
}
.navigationBarTitle("Trajet")
}
.onAppear{
self.webservice.getConnection(a: self.depart, b: self.destination)
}
}
}
}
行列表:
import SwiftUI
struct RowList: View {
var connection: Connection
func convertDate(from connection : String) -> String {
if let timeInterval = TimeInterval(connection) {
let date = Date(timeIntervalSince1970: timeInterval)
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
return formatter.string(from: date)
}
return ""
}
func convertString(from connection : String) -> String{
if let range = connection.range(of: "BE.NMBS.") {
let veh = connection[range.upperBound...]
return String(veh)
}
return "";
}
func convertMinute(from connection : String)-> String {
let duration: TimeInterval = Double(self.connection.departure.delay)!
let date = Date()
let cal = Calendar(identifier: .gregorian)
let start = cal.startOfDay(for: date)
let newDate = start.addingTimeInterval(duration)
let formatter = DateFormatter()
formatter.dateFormat = "mm"
let resultString = formatter.string(from: newDate)
return String("+" + resultString);
}
func convertMinuteArrival(from connection : String)-> String {
let duration: TimeInterval = Double(self.connection.arrival.delay)!
let date = Date()
let cal = Calendar(identifier: .gregorian)
let start = cal.startOfDay(for: date)
let newDate = start.addingTimeInterval(duration)
let formatter = DateFormatter()
formatter.dateFormat = "mm"
let resultString = formatter.string(from: newDate)
return String("+" + resultString);
}
var body: some View {
HStack{
VStack(alignment: .leading, spacing: 12){
HStack(spacing: 0){
Text(convertDate(from: connection.departure.time))
.font(.headline)
//.offset(y: 5)
if (connection.departure.delay != "0"){
if(Int(connection.departure.delay) ?? 0 >= 600 ){
Text(convertMinute(from: connection.departure.delay))
.foregroundColor(.red)
} else {
Text("+" + convertMinute(from:
String(connection.departure.delay)).suffix(1))
.foregroundColor(.red)
}
}
}
HStack{
if(connection.departure.platform.count == 2){
Text("Quai" + connection.departure.platform)
.font(.headline)
.foregroundColor(.blue)
}else
{
Text("Quai " + connection.departure.platform)
.font(.headline)
.foregroundColor(.blue)
}
}
}
Spacer()
.frame(minWidth: 0, maxWidth: 350 )
VStack {
if(connection.vias == nil)
{
Text("direct")
.foregroundColor(Color.blue)
Text(convertString(from: connection.departure.vehicle))
}
else{
ZStack{
Image("connection")
.frame(width: 40, height: 40 )
.cornerRadius(40)
Text(connection.vias?.number ?? "" )
.foregroundColor(Color.red)
.font(.caption)
.padding([.leading, .bottom], 40.0)
}
}
//Text(connection.vias?.via[0].vehicle)
}
Spacer()
.frame(minWidth: 0, maxWidth:350 )
VStack(alignment: .leading, spacing: 12){
HStack(spacing: 0){
Text(convertDate(from: connection.arrival.time))
if(connection.arrival.delay != "0") {
if(Int(connection.arrival.delay) ?? 0 >= 600 ){
Text(convertMinuteArrival(from: connection.arrival.delay))
.foregroundColor(.red)
} else {
Text("+" + convertMinuteArrival(from: String(connection.arrival.delay)).suffix(1))
.foregroundColor(.red)
}
}
}
HStack{
if(connection.arrival.platform.count == 2){
Text("Quai" + connection.arrival.platform)
.font(.headline)
.foregroundColor(.blue)
}else
{
Text("Quai " + connection.arrival.platform)
.font(.headline)
.foregroundColor(.blue)
}
}
}
Spacer()
}
}
}