Replies: 3 comments 1 reply
-
๐ง Topic3. Sample Code (๋ฒ์ธํธ)
import SwiftUI
// View์ ์ข
๋ฅ์ ๋ํ Enum
enum ViewType {
case one
case two
case three
case four
}
// MARK: ํ๋ฉด ์ ํ์ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด
class NavigationManager: ObservableObject {
@Published var path = NavigationPath()
// ํ๋ฉด ์ ํํ๊ธฐ
func navigate(to destination: ViewType) {
path.append(destination)
}
// ๋ฃจํธ๋ก ์ด๋ํ๊ธฐ
func poptoRoot() {
path.removeLast(path.count)
}
// ๋ค๋ก๊ฐ๊ธฐ
func pop() {
path.removeLast()
}
// ๋ด ๋ง์๋๋ก ์ปค์คํ
, 1 - 2 - 3 - 4 ์์ผ๋ก ์ด๋ํ๊ธฐ
func oneTwoThreeFour() {
path.append(ViewType.one)
path.append(ViewType.two)
path.append(ViewType.three)
path.append(ViewType.four)
}
}
struct ContentView: View {
@StateObject private var navigationManager = NavigationManager()
var body: some View {
NavigationStack(path: $navigationManager.path) {
VStack(spacing: 20) {
Text("์ฌ๊ธฐ๋ ๋ฃจํธ์
๋๋ค")
.font(.largeTitle)
.fontWeight(.bold)
.padding(.top, 50)
Button(action: {
// TODO: 1๋ฒ์ผ๋ก ์ด๋ํ๊ธฐ
navigationManager.navigate(to: .one)
}) {
Text("1๋ฒ์ผ๋ก ์ด๋ํ๊ธฐ")
.font(.title2)
.fontWeight(.semibold)
.padding()
.frame(maxWidth: .infinity)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal)
}
Button(action: {
// TODO: 2๋ฒ์ผ๋ก ์ด๋ํ๊ธฐ
navigationManager.navigate(to: .two)
}) {
Text("2๋ฒ์ผ๋ก ์ด๋ํ๊ธฐ")
.font(.title2)
.fontWeight(.semibold)
.padding()
.frame(maxWidth: .infinity)
.background(Color.green)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal)
}
Button(action: {
// TODO: 1 - 2 - 3 - 4 ์์ผ๋ก ์ด๋ํ๊ธฐ
navigationManager.oneTwoThreeFour()
}) {
Text("1 - 2 - 3 - 4 ์์ผ๋ก ์ด๋ํ๊ธฐ")
.font(.title2)
.fontWeight(.semibold)
.padding()
.frame(maxWidth: .infinity)
.background(Color.purple)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal)
}
}
.navigationDestination(for: ViewType.self) { value in
switch value {
case .one:
DetailView1()
.environmentObject(navigationManager)
case .two:
DetailView2()
.environmentObject(navigationManager)
case .three:
DetailView3()
.environmentObject(navigationManager)
case .four:
DetailView4()
.environmentObject(navigationManager)
}
}
}
}
}
struct DetailView1: View {
@EnvironmentObject var navigationManager: NavigationManager
var body: some View {
VStack(spacing: 20) {
Text("1๋ฒ ๋ทฐ ์
๋๋ค.")
.font(.largeTitle)
.fontWeight(.bold)
.padding(.top, 50)
Button {
// TODO: 3๋ฒ์ผ๋ก ์ด๋ํ๊ธฐ
navigationManager.navigate(to: .three)
} label: {
Text("3๋ฒ ๋ทฐ๋ก ์ด๋ํ๊ธฐ")
.font(.title2)
.fontWeight(.semibold)
.padding()
.frame(maxWidth: .infinity)
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal)
}
}
}
}
struct DetailView2: View {
@EnvironmentObject var navigationManager: NavigationManager
var body: some View {
VStack(spacing: 20) {
Text("2๋ฒ ๋ทฐ์
๋๋ค.")
.font(.largeTitle)
.fontWeight(.bold)
.padding(.top, 50)
Button {
// TODO: 4๋ฒ์ผ๋ก ์ด๋ํ๊ธฐ
navigationManager.navigate(to: .four)
} label: {
Text("4๋ฒ ๋ทฐ๋ก ์ด๋ํ๊ธฐ")
.font(.title2)
.fontWeight(.semibold)
.padding()
.frame(maxWidth: .infinity)
.background(Color.purple)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal)
}
}
}
}
struct DetailView3: View {
@EnvironmentObject var navigationManager: NavigationManager
var body: some View {
Text("3๋ฒ ๋ทฐ์
๋๋ค.")
.font(.largeTitle)
.fontWeight(.bold)
.padding(.top, 50)
Button {
// TODO: ๋ฃจํธ๋ก ์ด๋ํ๊ธฐ
navigationManager.poptoRoot()
} label: {
Text("๋ฃจํธ๋ก ์ด๋ํ๊ธฐ")
.font(.title2)
.fontWeight(.semibold)
.padding()
.frame(maxWidth: .infinity)
.background(Color.purple)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal)
}
}
}
struct DetailView4: View {
@EnvironmentObject var navigationManager: NavigationManager
var body: some View {
Text("4๋ฒ ๋ทฐ ์
๋๋ค.")
.font(.largeTitle)
.fontWeight(.bold)
.padding(.top, 50)
Button {
// TODO: ๋ค๋ก ๊ฐ๊ธฐ
navigationManager.pop()
} label: {
Text("๋ค๋ก ๊ฐ๊ธฐ ์์ด ๋ค๋ก๊ฐ๊ธฐ?")
.font(.title2)
.fontWeight(.semibold)
.padding()
.frame(maxWidth: .infinity)
.background(Color.purple)
.foregroundColor(.white)
.cornerRadius(10)
.padding(.horizontal)
}
}
}
#Preview {
ContentView()
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
โ๏ธ Topic 4. ๋ง์ฝ ๊ฐ ํ๋ฉด์ ์ํ๋ฅผ ๋ฐ์ธ๋ฉํ๋ ค๋ฉด// ๋ชจ๋ธ
struct Gus: Identifiable, Hashable {
var id: UUID
var name: String
init(name: String = "") {
self.id = UUID()
self.name = name
}
}
import SwiftUI
// Hashable ์ถ๊ฐ
enum ViewType: Hashable {
case .one
case .two
case .three
case .four(gus: Gus)
}
struct ContentView: View {
@State private var gus: [Gus] = [Gus(name:"์์ ์ ์ผ"), Gus(name:"๋น ๋")]
@StateObject private var navigationManager = NavigationManager()
var body: some View {
NavigationStack(path: $navigationManager.path) {
List {
ForEach(gus) { g in
navigationManager.navigate(to: .four(gus:g))
} label: {
Text("4๋ฒ ํ๋ฉด์ผ๋ก ๊ฐ๊ธฐ")
}
}
.navigationDestination(for: ViewType.self) { viewType in
switch viewType {
case .one:
DetailView1(gus: $gus)
case .two:
DetailView2()
case .three:
DetailView3()
case .four(let friend):
DetailView4(friend: friend)
}
}
}
}
}
struct DetailView1: View {
@Binding var gus: [Friend]
var body: some View {
List {
ForEach(friends) { friend in
Text(friend.name)
}
}
}
}
struct DetailView4: View {
@EnvironmentObject var navigationManager: NavigationManager
@Binding var gus: Gus
var body: some View {
Text("4๋ฒ ๋ทฐ ์
๋๋ค. : \(gus.name)")
.font(.largeTitle)
.fontWeight(.bold)
.padding(.top, 50)
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
ํ ..โจ ๊ฐ์ฌํฉ๋๋ค!! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
โ๏ธ Overview
๊ธ์ ์ฝ๋ค๊ฐ ์ดํด๊ฐ ์๋๋๋ผ๋ ์ผ๋จ ์ญ~ ์ฝ์ด๋ณด์!
NavigationStack์ ์ฌ์ฉํด ํ๋ฉด ์ ํ์ ์์์ ์ ๋ง๋ค๊ณ
NavigationLink๋ก ๋ค๋ฅธ ํ๋ฉด์ผ๋ก ์ด๋ํ๋ ๋ฐฉ๋ฒ์ ์ตํ๋ฉฐ
NavigationPath๋ฅผ ํ์ฉํ ๋์ ํ๋ฉด ์ ํ๊น์ง ๋ฐฐ์๋ด ๋๋ค.
๋ง์ง๋ง์ผ๋ก, ํ๋ฉด ์ ํ ๋ก์ง์ ํ๋์ ๊ฐ์ฒด๋ก ๊ด๋ฆฌํ๋ ๊ตฌ์กฐ๋ ํจ๊ป ์์๋ด ๋๋ค. (์ค๊ธ์์ฉ)
โ๏ธ Before the Start...
๐ก ๋ง์ธ๋์ ์ ํโฆ
SwiftUI์ Navigation Stack์ ๋ฐ์ดํฐ์ ์ ํ ๊ด์ ์์ ์ ๊ทผํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
์ผ๋ฐ์ ์ผ๋ก ์ฐ๋ฆฌ๊ฐ ํ๋ฉด์ ์ ํํ๋ค๊ณ ์๊ฐ์ ํ๋ค๋ฉด,
์์ ๊ฐ์ ์ฌ๊ณ ์ ํ๋ฆ์ ํ๊ธฐ ์ฝ์ง๋ง,
[์ฐธ๊ณ ]
์์ ๊ฐ์ ์ฌ๊ณ ์ ํ๋ฆ์ด ์ ์ธํ, ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ํจ๋ฌ๋ค์์ ์งํฅํ๋ SwiftUI์ ๋ ์ ํฉํฉ๋๋ค. (๋ชฐ๋ผ๋ ๋จ)
1. NavigationStack
NavigationStack์ ํ๋ฉด ์ ํ์ ์์์ (root view)์ ๋ง๋ค์ด์ฃผ๋ ์ปจํ ์ด๋์ ๋๋ค.
1.1 NavigationStack์ 2๊ฐ์ง ์ข ๋ฅ
Xcode์ Navigation Stack์ ์ ๋ ฅํ๋ฉด 2๊ฐ์ง ์์ฑ์๊ฐ ์๋ค๋ ๊ฒ์ ํ์ธ ํ ์ ์์ต๋๋ค.
-> ๋ฌด์จ ๋ง์ธ์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง, ๋์ถฉ path๋ฅผ ์ธ ์๋, ์ ์ธ ์๋ ์๋ค๋ ๋๋์ด ๋ญ๋๋ค.
1.2 NavigationLink
NavigationLink๋ ์ฌ์ฉ์๊ฐ ๋๋ฅด๋ฉด ๋ค๋ฅธ ํ๋ฉด์ผ๋ก ์ ํ๋๋ ๋ฒํผ ์ญํ ์ ํฉ๋๋ค.
SwiftUI๋ ์ด ๋งํฌ๋ฅผ ๋๋ฅผ ๋๋ง๋ค ์๋์ผ๋ก ๋ค์ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๋ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค์ด์ค๋๋ค.
1.3 NavigationDestination
NavigationDestination์ NavigationStack์์ ํ๋ฉด์ ์ ํํ ๋์ ๋ทฐ(Destination View) ๋ฅผ ์ง์ ํด์ฃผ๋ ์ญํ ์ ํฉ๋๋ค.
์ด ๊ฒฝ์ฐ, ๋ฐ์ ํ ์ดํฐ์ ํ์ ์ ๋ฐ๋ผ์, ๋ชฉ์ ์ง๋ฅผ ์ค์ ํด์ค๋๋ค.
์ฝ๊ฒ ๋งํด, NavigationLink๋ฅผ ํตํด ๋๊ธด ๋ฐ์ดํฐ๊ฐ ์ด๋ค ํ๋ฉด๊ณผ ์ฐ๊ฒฐ๋์ด์ผ ํ๋์ง๋ฅผ SwiftUI์๊ฒ ์๋ ค์ฃผ๋ ๊ธฐ๋ฅ์ ๋๋ค.
์ ์ฝ๋๋ ์ด๋ฐ ์๋ฏธ์ ๋๋ค:
๋ฐ๋ผ์ NavigationLink(value:)๋ก ๊ฐ์ ๋๊ธฐ๊ธฐ๋ง ํ๋ฉด,
์์คํ ์ ์๋์ผ๋ก ์๋ง์(ํด๋น ํ์ ์ ๋ง๋) Destination View๋ฅผ ์ฐพ์ ์ฐ๊ฒฐํด์ค๋๋ค.
โ๏ธย Topic 1. Navigation Path๋ฅผ ์ฌ์ฉํ์ง ์๋ ๋ฐฉ๋ฒ
๐ง Topic1. Sample Code (๋ณต์ฌํด์ ์ฐ์ตํด๋ณด์ธ์.)
Q. ์๋ ๊ทผ๋ฐ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ณ ์ถ์ง ์์ ์๋ ์์์์?
๋ฐ์ดํฐ์ ์ ํ ์์ด ํ๋ฉด์ ์ ํํ ์๋ ์๋ค.

๋ฉ์๋๋ฅผ ๋ถ์ํด๋ณด๋ฉด, 2๊ฐ์ ํ๋ผ๋ฏธํฐ๋ฅผ ํ์๋ก ํฉ๋๋ค.
์ ๋ฌํด์ผ ํ ๋ฐ์ดํฐ๊ฐ ์์ผ๋๊น, .Destination์ ์ค์ ํ์ง ์์๋ ๋๊ฒ ์ฃ ?
โ๏ธย Topic 2. Navigation Path๋ฅผ ํ์ฉํ ๋์ ์ฌ์ฉ๋ฒ
์ด๋ฒ์๋ ๋์ ์ผ๋ก Navigation์ ์ ํํด ๋ณด๊ฒ ์ต๋๋ค. (๋์ ์ด ๋ญ ์ง ๋ชฐ๋ผ๋ ๋จ. ๋๋๋ง ๊ฐ์ ธ๊ฐ์ธ์)
์ด์ ๋ถํฐ ์ฐ๋ฆฌ๋ Navigation Path์๋ง ์ง์คํ๋ฉด ๋ฉ๋๋ค.
Navigation Path๋ ์ง๊ธ ํ์ฌ ํ๋ฉด ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๋ฐฐ์ด์ ๋๋ค.
์์คํ ์ Navigation Path๋ฅผ ๊ด์ฐฐํ๋ฉฐ, Path์ ๋ง๊ฒ ํ๋ฉด์ ๊ตฌ์ฑํฉ๋๋ค.
[์์๋ก ์์๋ณด๊ธฐ]
๐ง Topic2. Sample Code (๋ณต์ฌํด์ ์ฐ์ตํด๋ณด์ธ์.)
โ๏ธย Topic 3. ๋ถํธํ๋ฐ์? ์กฐ๊ธ ๋ ์ธ๋ จ๋๊ฒ ์ฌ์ฉํ ์ ์์๊น์? (Advanced)
โ ์์ ๋ฅผ ๋ฐ๋ผ ํ๋ฉด์ ์๊ธฐ๋ ๋ถํธํ ์ ๋ค์ ํ๋ฒ ์ ๋ฆฌํด๋ณผ๊ฒ์.
1. ํ๋ฉด(View)์ ๋ง๋ค ๋๋ง๋ค NavigationPath๋ฅผ ๊ผญ ๋๊ฒจ์ค์ผ ํด์
2. View๊ฐ ๋๋ฌด ๋ง์ ์ฑ ์์ ์ง๊ฒ ๋ผ์ (๋ชฐ๋ผ๋ ๋จ)
์ด๊ฒ ๋ฌด์จ ๋ฌธ์ ๋๋ฉดโฆ
๊ทธ๋์ ์ฐ๋ฆฌ๋ ํ๋ฉด ์ ํ์ ์ฑ ์์ ๋ทฐ์์ ๋ผ์ด๋ด์,
์ ๋ฌธ์ ์ผ๋ก Navigation๋ง ๊ด๋ฆฌํ๋ ๊ฐ์ฒด์๊ฒ ๋งก๊ฒจ ๋ณด๋ ์ฐ์ต์ ํด๋ณด๊ฒ ์ต๋๋ค.
์ด๊ฑด ๋ง์น ๊ธธ ์๋ด๋ ๋ค๋น๊ฒ์ด์ ์๊ฒ ๋งก๊ธฐ๊ณ , ์ด์ ์๋ ์ด์ ์๋ง ์ง์คํ๋ ๊ฒ๊ณผ ๊ฐ์์.
[NavigationManager(์์)์ ์๊ตฌ ์ฌํญ]
๐ง Topic3. Sample Code (๋ณต์ฌํด์ ์ฐ์ตํด๋ณด์ธ์.)
โ๏ธ Summary
โ ๋ด์ฉ ์์ฝ
Beta Was this translation helpful? Give feedback.
All reactions