I try to learn SwiftUI but I don't understand why this works in the Apple tutorial, but not in my own project. I want to use my own Design and that means that the NavigationLink
looks like a normal Button. The Problem is that this works only once and when I use the < Back button I can't use the "Edit" button a second time.
import SwiftUI
import Foundation
struct TimeTrackerView: View {
@ObservedObject var timeTracker = TimeTracker()
@State private var isStart = false
var body: some View {
NavigationView {
ZStack {
Color.black
.edgesIgnoringSafeArea(.all)
VStack {
Spacer()
Text( timeString(time: self.timeTracker.time) )
.font(.largeTitle)
.foregroundColor(.white)
.padding(50)
Spacer()
HStack {
Group {
Button(action: {
self.isStart.toggle()
if self.isStart {
self.timeTracker.startTimer()
} else {
self.timeTracker.stopTimer()
}
}) {
if isStart {
Text("Stop")
.foregroundColor(.red)
} else {
Text("Start")
.foregroundColor(.white)
}
}
Spacer()
// BUG: Button works only once
NavigationLink(destination: TimeDetail()) {
Text("Edit")
.foregroundColor(.white)
}
}
}
.font(.title).padding(50)
}
}
.navigationBarTitle("Navigation")
}
}
}
func timeString(time: Int) -> String {
let hours = time / 3600
let minutes = time / 60 % 60
let seconds = time % 60
return String(format:"%1ih %1im %1is", hours, minutes, seconds)
}
struct TimeTrackerView_Previews: PreviewProvider {
static var previews: some View {
TimeTrackerView()
}
}
and this is the TimeDetail view.
import SwiftUI
struct TimeDetail: View {
var body: some View {
Group {
Text("HI, i'm View")
}.navigationBarTitle("Navi", displayMode: .inline)
}
}
struct TimeDetail_Previews: PreviewProvider {
static var previews: some View {
TimeDetail()
}
}
I find out that when I use a List
with NavigationLink
s it works. But not with a single button.