I have the following code someone presses on the Table and its displays the current time which is the arrival time of a customer.
I want to display the time they must leave by, next to the current time this will always be 1.5 hours ahead I can not work out how to do this. everything I have tried comes back with an error.
Still new to Xcode
any help would be great
import SwiftUI
struct TimeListView: View {
@State var tableOne = false
@State var tableTwo = false
@State var tableThree = false
var body: some View {
// Title
VStack {
Text("Arrival Times")
.font(.title)
.fontWeight(.bold)
// List View
List {
// Table 1
HStack {
Button(action: {
self.tableOne.toggle()
}, label: {
Text("Table 1 -")
})
if tableOne {
Text(getCurrentTime())
}
}
// Table 2
HStack {
Button(action: {
self.tableTwo.toggle()
}, label: {
Text("Table 2 -")
})
if tableTwo {
Text(getCurrentTime())
}
}
// Table 3
HStack {
Button(action: {
self.tableThree.toggle()
}, label: {
Text("Table 3 -")
})
if tableThree {
Text(getCurrentTime())
}
}
}
}
}
}
struct TimeListView_Previews: PreviewProvider {
static var previews: some View {
TimeListView()
}
}
// Get Current Time Function
func getCurrentTime() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_UK_POSIX")
dateFormatter.dateFormat = "HH:mm"
return dateFormatter.string(from: Date())
2
Answers
you need make a date and add 1.5 hour to it, also you forgot create 3 deferent State for them.
You’ll probably get a half dozen different ways of doing this but this version allows for adaptive/reusable code. You can easily add a Table by adding one to the
var tables: [Table]
everything would adjust automatically.