_Rey
프로그래뭄
_Rey
전체 방문자
오늘
어제
  • 분류 전체보기 (118)
    • Life (2)
    • iOS (49)
      • iOS (13)
      • Swift (19)
      • UIKit (0)
      • RxSwift (6)
      • SwiftUI (11)
    • Design Pattern (14)
      • GoF - Creational Patterns (6)
      • GoF - Structural Patterns (7)
    • Data Structure & Algorithm (48)
      • Data Structure (3)
      • Algorithm (8)
      • Advent of Code 2024 (1)
      • 코테 스터디 TIL (36)
    • English (2)
    • Book (2)
      • Clean Architecture (2)

블로그 메뉴

  • Instagram
  • Github
  • Naver Blog

공지사항

  • Hello, Programoom

인기 글

hELLO · Designed By 정상우.
_Rey

프로그래뭄

iOS/SwiftUI

[SwiftUI] @Environment, @EnvironmentObject, @FocusedBinding, @FocusedValue

2022. 4. 19. 05:43

@Environment

이름처럼 환경변수입니다.

// 정해진 Key Path로 환경 변수를 컨트롤 할 수 있다.
@Environment(\.colorScheme) var colorScheme: ColorScheme
@Environment(\.horizontalSizeClass) var horizontalSizeClass

다크모드 등 환경에 대한 변수 설정이 가능하다

 

@EnvironmentObject

해당 Wrapper은 환경 객체로 ObservableObject Protocol를 따라야합니다.

class ExampleEnvironmentObject: ObservableObject {
    @Published var attribute = "ExampleEnvironmentObject"
}

struct SecondView: View {
    @EnvironmentObject var environmentObject: ExampleEnvironmentObject

    var body: some View {
    	...
    }
}

struct FirstView: View {
    @StateObject var environmentObject = ExampleEnvironmentObject()

    var body: some View {
    	...
        .environmentObject(environmentObject)
    }
}

@StataObject를 environmentObject modifier를 통해 전달한다.

단순히 자식 뷰에 환경객체를 설정하는 것이 아닌 부모-자식 관계가 깊은 뷰에 객체를 전달해야할 때,

유용하게 쓰일 수 있겠다.

 

@FocusedBinding, @FocusedValue

TextField 같이 키보드 포커싱이 있는 부분에서 사용된다.

사용법은 너무 복잡해서 아직 이해를 못했다....

나중에 예제 코드를 작성해보자.

 

@FocusState

iOS 15.0 이상 사용 가능하며, @FocusedBinding, @FocusedValue 처럼 쓰이지만 훨씬 편하다.

struct LoginForm {
    enum Field: Hashable {
        case username
        case password
    }

    @State private var username = ""
    @State private var password = ""
    @FocusState private var focusedField: Field?

    var body: some View {
        Form {
            TextField("Username", text: $username)
                .focused($focusedField, equals: .username)

            SecureField("Password", text: $password)
                .focused($focusedField, equals: .password)

            Button("Sign In") {
                if username.isEmpty {
                	// TextField로 포커싱
                    focusedField = .username
                } else if password.isEmpty {
                	// SecureField로 포커싱
                    focusedField = .password
                } else {
                    handleLogin(username, password)
                }
            }
        }
    }
}

하면 안되는 코드

// 하나의 Content에 여러 값을 Binding 시키는 것
TextField("Full Name", ...)
	.focused($focusedField, equals: .name)
	.focused($focusedField, equals: .fullName)
    
// 동일한 값을 다른 두 Content에 Binding 시키는 것
TextField("Name", ...)
	.focused($focusedField, equals: .name)
TextField("Full Name", ...)
	.focused($focusedField, equals: .name)

 

저작자표시 비영리 변경금지 (새창열림)

'iOS > SwiftUI' 카테고리의 다른 글

[SwiftUI] @GestureState, @Namespace, @ScaledMetric  (0) 2022.04.19
[SwiftUI] @UIApplicationDelegateAdaptor, @NSApplicationDelegateAdaptor  (0) 2022.04.19
[SwiftUI] @Published, @ObservedObject, @StateObject  (0) 2022.04.19
[SwiftUI] @State, @Binding, @AppStorage, @SceneStroage, @FetchRequest  (0) 2022.04.18
[SwiftUI] ForEach의 id에 대해서  (0) 2022.04.18
    'iOS/SwiftUI' 카테고리의 다른 글
    • [SwiftUI] @GestureState, @Namespace, @ScaledMetric
    • [SwiftUI] @UIApplicationDelegateAdaptor, @NSApplicationDelegateAdaptor
    • [SwiftUI] @Published, @ObservedObject, @StateObject
    • [SwiftUI] @State, @Binding, @AppStorage, @SceneStroage, @FetchRequest
    _Rey
    _Rey
    잘 배워서 다 남주자

    티스토리툴바