개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.06.07 09:33

UIMenu::appendPreferences와 관련된 질문

목록
  • 버전컨트롤러 오래 전 2025.06.07 09:33 인기
  • 119
    1
제가 현재 Swift UI에서 UIMenu를 사용하여 Context Menu를 구현 중인데, appendPreferences 메서드를 사용하여 Preference Items를 추가하려고 합니다.

appendPreferences 메서드는 어떻게 사용해야 하는지 정확히 모르겠고, Preference Items를 추가하는 방법에 대해 알려주세요.

또한, Preference Items의 기본 설정값을 어떻게 구현하는지 알려주세요.

제가 작성한 코드는 다음과 같습니다.

swift

@main

struct MyApp: App {

    var body: some Scene {

        WindowGroup {

            ContentView()

                .environment(.uiPreferences, .init())

        }

    }

}



struct ContentView: View {

    @Environment(.uiPreferences) var preferences

    

    var body: some View {

        Button(action: {

            // Context Menu를 띄우는 코드

        }) {

            Text("Button")

        }

        .contextMenu {

            HStack {

                Button(action: {

                    // Preference Item 1을 클릭했을 때 동작

                }) {

                    Text("Preference Item 1")

                }

                Button(action: {

                    // Preference Item 2을 클릭했을 때 동작

                }) {

                    Text("Preference Item 2")

                }

            }

        }

    }

}



위 코드에서 appendPreferences 메서드를 사용하여 Preference Items를 추가하는 방법을 알려주세요.

    댓글목록

    profile_image
    나우호스팅  오래 전



    UIMenu의 appendPreferences 메서드는 Preference Items를 추가하는 데 사용됩니다.

    Preference Items를 추가하려면, 우선 Preference Store를 생성해야 합니다. Preference Store는 Preference Items의 기본 설정값을 관리합니다.

    #hostingforum.kr
    swift
    
    @main
    
    struct MyApp: App {
    
        var body: some Scene {
    
            WindowGroup {
    
                ContentView()
    
                    .environment(.uiPreferences, .init())
    
            }
    
        }
    
    }
    
    
    
    struct ContentView: View {
    
        @Environment(.uiPreferences) var preferences
    
        
    
        var body: some View {
    
            Button(action: {
    
                // Context Menu를 띄우는 코드
    
            }) {
    
                Text("Button")
    
            }
    
            .contextMenu {
    
                HStack {
    
                    Button(action: {
    
                        // Preference Item 1을 클릭했을 때 동작
    
                    }) {
    
                        Text("Preference Item 1")
    
                    }
    
                    Button(action: {
    
                        // Preference Item 2을 클릭했을 때 동작
    
                    }) {
    
                        Text("Preference Item 2")
    
                    }
    
                }
    
            }
    
        }
    
    }
    
    


    Preference Store를 생성하려면, Preference Items를 정의해야 합니다. Preference Items는 PreferenceKey를 사용하여 정의됩니다.

    #hostingforum.kr
    swift
    
    struct PreferenceKey: PreferenceKey {
    
        static var defaultValue: Bool = false
    
        
    
        static func reduce(value: inout Bool, nextValue: () -> Bool) {
    
            value = nextValue()
    
        }
    
    }
    
    
    
    struct ContentView: View {
    
        @Environment(.uiPreferences) var preferences
    
        
    
        var body: some View {
    
            Button(action: {
    
                // Context Menu를 띄우는 코드
    
            }) {
    
                Text("Button")
    
            }
    
            .contextMenu {
    
                HStack {
    
                    Button(action: {
    
                        preferences[PreferenceKey.self] = true
    
                    }) {
    
                        Text("Preference Item 1")
    
                    }
    
                    Button(action: {
    
                        preferences[PreferenceKey.self] = false
    
                    }) {
    
                        Text("Preference Item 2")
    
                    }
    
                }
    
            }
    
        }
    
    }
    
    


    Preference Items의 기본 설정값을 구현하려면, Preference Store의 defaultValue를 사용합니다.

    #hostingforum.kr
    swift
    
    struct PreferenceKey: PreferenceKey {
    
        static var defaultValue: Bool = true
    
        
    
        static func reduce(value: inout Bool, nextValue: () -> Bool) {
    
            value = nextValue()
    
        }
    
    }
    
    


    이제 Preference Items를 추가하는 코드는 다음과 같습니다.

    #hostingforum.kr
    swift
    
    struct ContentView: View {
    
        @Environment(.uiPreferences) var preferences
    
        
    
        var body: some View {
    
            Button(action: {
    
                // Context Menu를 띄우는 코드
    
            }) {
    
                Text("Button")
    
            }
    
            .contextMenu {
    
                HStack {
    
                    Button(action: {
    
                        preferences[PreferenceKey.self] = true
    
                    }) {
    
                        Text("Preference Item 1")
    
                    }
    
                    Button(action: {
    
                        preferences[PreferenceKey.self] = false
    
                    }) {
    
                        Text("Preference Item 2")
    
                    }
    
                }
    
            }
    
        }
    
    }
    
    


    이 코드는 Preference Items를 추가하고, Preference Items의 기본 설정값을 구현합니다.

    2025-06-07 09:34

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 37,391건 / 8 페이지

검색

게시물 검색