{"id":32733,"date":"2024-11-01T09:11:09","date_gmt":"2024-11-01T09:11:09","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32733"},"modified":"2024-11-01T11:23:58","modified_gmt":"2024-11-01T11:23:58","slug":"swiftui-style-iphone-app-development-05-selecting-desired-items-using-picker-view","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32733\/","title":{"rendered":"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View"},"content":{"rendered":"<p><body><\/p>\n<p>A few days ago, you were curious about how to use the Picker View, which allows users to select desired items from a view. In SwiftUI, the Picker View is very intuitive and a key component for enhancing user experience. In this article, we will take a detailed look at the basics of SwiftUI&#8217;s Picker View, from simple to complex usage.<\/p>\n<h2>1. Basics of SwiftUI and Picker View<\/h2>\n<p>SwiftUI is Apple&#8217;s new UI framework that helps developers easily build user interfaces in a Declarative way. SwiftUI is optimized for creating applications that run on various devices, with code that is intuitively and concisely written.<\/p>\n<p>The Picker View is a UI component that allows users to select one item from multiple options. It usually appears in a dropdown form, and the selected item can be used immediately elsewhere. In SwiftUI, implementing a Picker View is straightforward.<\/p>\n<h2>2. Basic Usage of Picker View<\/h2>\n<h3>2.1 Creating a Basic Picker View<\/h3>\n<p>First, let&#8217;s look at the basic way to create a Picker View in SwiftUI. The code below is an example of creating a simple Picker View.<\/p>\n<pre><code>import SwiftUI\n\nstruct ContentView: View {\n    @State private var selectedItem = \"Apple\"\n    let items = [\"Apple\", \"Banana\", \"Cherry\", \"Grape\"]\n\n    var body: some View {\n        VStack {\n            Text(\"Selected Fruit: \\(selectedItem)\")\n                .font(.largeTitle)\n\n            Picker(\"Select a Fruit\", selection: $selectedItem) {\n                ForEach(items, id: \\.self) { item in\n                    Text(item).tag(item)\n                }\n            }\n            .pickerStyle(MenuPickerStyle())  \/\/ Choose picker style\n            .padding()\n        }\n    }\n}\n\nstruct ContentView_Previews: PreviewProvider {\n    static var previews: some View {\n        ContentView()\n    }\n}<\/code><\/pre>\n<p>In the code above, we use a `@State` variable to store the selected item. The Picker View is created using the `Picker` structure, and we add each item by iterating through our item list with `ForEach`.<\/p>\n<h3>2.2 Choosing a Picker Style<\/h3>\n<p>SwiftUI offers various picker styles to provide the picker to users in the desired way. The most common styles are:<\/p>\n<ul>\n<li><strong>MenuPickerStyle<\/strong>: Dropdown list format<\/li>\n<li><strong>SegmentedPickerStyle<\/strong>: Items listed in button form<\/li>\n<li><strong>WheelPickerStyle<\/strong>: Item selection in wheel format<\/li>\n<\/ul>\n<p>The code below is an example using `SegmentedPickerStyle`.<\/p>\n<pre><code>Picker(\"Select a Fruit\", selection: $selectedItem) {\n    ForEach(items, id: \\.self) { item in\n        Text(item).tag(item)\n    }\n}\n.pickerStyle(SegmentedPickerStyle())  \/\/ Choose segment style<\/code><\/pre>\n<h2>3. Utilizing Picker View<\/h2>\n<h3>3.1 Picker View for Multiple Selections<\/h3>\n<p>Basic Picker Views are designed to select a single item. However, if you need multiple selection functionality, you can create an array of selected items. Here\u2019s an example implementing multiple selections.<\/p>\n<pre><code>struct MultiPickerView: View {\n    @State private var selectedItems = Set<String>()\n    let items = [\"Apple\", \"Banana\", \"Cherry\", \"Grape\"]\n\n    var body: some View {\n        VStack {\n            Text(\"Selected Fruits: \\(selectedItems.joined(separator: \", \"))\")\n                .font(.largeTitle)\n\n            Picker(\"Select a Fruit\", selection: $selectedItems) {\n                ForEach(items, id: \\.self) { item in\n                    Text(item).tag(item)\n                }\n            }\n            .pickerStyle(MultipleSelectionPickerStyle())  \/\/ Multiple selection style\n            .padding()\n        }\n    }\n}<\/code><\/pre>\n<p>In the example above, we use a `Set` to store the selected items, allowing users to easily see the items they have selected.<\/p>\n<h3>3.2 Creating a Custom Picker View<\/h3>\n<p>Sometimes the built-in Picker View may not be sufficient. In such cases, you may need to create a custom Picker View. The next example shows how to implement a picker using a custom view.<\/p>\n<pre><code>struct CustomPicker: View {\n    @Binding var selected: String\n    let items: [String]\n\n    var body: some View {\n        HStack {\n            Text(selected)\n                .padding()\n                .background(Color.gray.opacity(0.3))\n                .cornerRadius(8)\n                .onTapGesture {\n                    \/\/ Implement the logic that appears when the picker is clicked here\n                }\n            Spacer()\n        }\n    }\n}<\/code><\/pre>\n<p>The code above creates a structure for a custom picker view that displays selectable items. This structure uses `@Binding` to allow the selected item to be managed externally.<\/p>\n<h2>4. Connecting Picker View to Data<\/h2>\n<p>Using the Picker View practically can connect different data sources to provide a richer experience to users. For instance, you can connect data fetched from a JSON API to the picker.<\/p>\n<pre><code>struct API {\n    static let fruits = [\"Apple\", \"Banana\", \"Cherry\", \"Grape\"]\n}\n\nstruct DataPickerView: View {\n    @State private var selectedFruit = API.fruits[0]\n    let fruits = API.fruits\n\n    var body: some View {\n        Picker(\"Select a Fruit\", selection: $selectedFruit) {\n            ForEach(fruits, id: \\.self) { fruit in\n                Text(fruit).tag(fruit)\n            }\n        }\n        .pickerStyle(MenuPickerStyle())\n        .padding()\n    }\n}<\/code><\/pre>\n<p>Here, we use a structure called `API` to provide isolated data. This makes it easier to handle various data sources.<\/p>\n<h2>5. Optimization and Considerations<\/h2>\n<h3>5.1 Performance Optimization<\/h3>\n<p>When using Picker Views in SwiftUI, performance should be considered. The rendering of views can be slow depending on the size and complexity of the data list.<\/p>\n<ul>\n<li>If the number of items is large, performance can be improved by applying lazy loading techniques.<\/li>\n<li>It\u2019s important to optimize memory usage by loading only the necessary data.<\/li>\n<\/ul>\n<h3>5.2 Accessibility<\/h3>\n<p>It is crucial to design UI elements to be accessible to all users. Here are a few methods to improve accessibility in Picker Views.<\/p>\n<ul>\n<li>It is necessary to use appropriate labels for screen readers to understand.<\/li>\n<li>Testing with users should be done to verify and improve the user experience.<\/li>\n<\/ul>\n<h2>6. Conclusion<\/h2>\n<p>This article explored various methods and possibilities for using Picker Views in SwiftUI. The Picker View provides users with an intuitive way to select information, enhancing the overall user experience. We hope you learned how to improve app interfaces by using SwiftUI\u2019s picker and manage user selections more easily.<\/p>\n<p>With a deep understanding of Picker Views, we hope this helps you in your next iPhone app development. Try to utilize various ways of handling Picker Views to provide a consistent user experience across the operating system and devices.<\/p>\n<p>If you have further questions or need to discuss more in-depth topics, please leave a comment. Let&#8217;s have a discussion together!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few days ago, you were curious about how to use the Picker View, which allows users to select desired items from a view. In SwiftUI, the Picker View is very intuitive and a key component for enhancing user experience. In this article, we will take a detailed look at the basics of SwiftUI&#8217;s Picker &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32733\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-32733","post","type-post","status-publish","format-standard","hentry","category-swift-iphone-app-development-swiftui"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/32733\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"A few days ago, you were curious about how to use the Picker View, which allows users to select desired items from a view. In SwiftUI, the Picker View is very intuitive and a key component for enhancing user experience. In this article, we will take a detailed look at the basics of SwiftUI&#8217;s Picker &hellip; \ub354 \ubcf4\uae30 &quot;SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32733\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:11:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:23:58+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32733\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32733\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View\",\"datePublished\":\"2024-11-01T09:11:09+00:00\",\"dateModified\":\"2024-11-01T11:23:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32733\/\"},\"wordCount\":687,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift iPhone app development (SwiftUI)\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32733\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32733\/\",\"name\":\"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:11:09+00:00\",\"dateModified\":\"2024-11-01T11:23:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32733\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32733\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32733\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/32733\/","og_locale":"ko_KR","og_type":"article","og_title":"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"A few days ago, you were curious about how to use the Picker View, which allows users to select desired items from a view. In SwiftUI, the Picker View is very intuitive and a key component for enhancing user experience. In this article, we will take a detailed look at the basics of SwiftUI&#8217;s Picker &hellip; \ub354 \ubcf4\uae30 \"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View\"","og_url":"https:\/\/atmokpo.com\/w\/32733\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:11:09+00:00","article_modified_time":"2024-11-01T11:23:58+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32733\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32733\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View","datePublished":"2024-11-01T09:11:09+00:00","dateModified":"2024-11-01T11:23:58+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32733\/"},"wordCount":687,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift iPhone app development (SwiftUI)"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32733\/","url":"https:\/\/atmokpo.com\/w\/32733\/","name":"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:11:09+00:00","dateModified":"2024-11-01T11:23:58+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32733\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32733\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32733\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"SwiftUI Style, iPhone App Development: 05 Selecting Desired Items Using Picker View"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=32733"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32733\/revisions"}],"predecessor-version":[{"id":32734,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32733\/revisions\/32734"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}