Dasturiy navigatsiya
SwiftUI navigatsiya: dasturiy navigatsiya
NavigationPath va tiplashtirilgan manzillar yordamida navigatsiyani kod orqali boshqaring.
Path’ni boshqarish
NavigationPath navigatsiya yo‘lini (path) boshqaradi.
Misol
import SwiftUI
struct NumberDetail: View { let n: Int; var body: some View { Text("Number: \(n)") } }
struct ProgrammaticPathDemo: View {
@State private var path: [Int] = []
var body: some View {
NavigationStack(path: $path) {
VStack(spacing: 12) {
Button("Go to 42") { path.append(42) }
Button("Back") { if !path.isEmpty { _ = path.removeLast() } }
}
.navigationDestination(for: Int.self) { n in NumberDetail(n: n) }
.navigationTitle("Programmatic")
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View { ProgrammaticPathDemo() }
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene { WindowGroup { ContentView() } }
}
Yuqoridagi misolda navigatsiya yo‘lini boshqarish uchun NavigationPath ishlatilgan.
Qayta o‘rnatish va bir nechta push
Yo‘lni qayta o‘rnating (reset) va bir nechta manzilni push qiling.
Misol
import SwiftUI
struct ResetMultiPushDemo: View {
@State private var path: [Int] = []
var body: some View {
NavigationStack(path: $path) {
VStack(spacing: 12) {
Button("Reset to root") { path.removeAll() }
Button("Go to 1 → 2 → 3") { path.append(contentsOf: [1,2,3]) }
}
.navigationDestination(for: Int.self) { n in Text("Screen #\(n)") }
.navigationTitle("Reset & Multi-Push")
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View { ResetMultiPushDemo() }
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene { WindowGroup { ContentView() } }
}
Yuqoridagi misolda navigatsiya yo‘lini boshqarish uchun NavigationPath ishlatilgan.
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
