LAST CHANCE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 16 Creating Views in a Loop

Forums > 100 Days of SwiftUI

I tried following along with the code that the hackingwithswift instructor wrote in my own xcode file and it does not work the same. When I click on select your student it gives me a drop down of all the students it does not go to the navigation view with the back button. I typed it in exactly as the instructor and it does not work. I notice in the simuIator the students have an up and down arrow at the end of their name instead of an arror pointing to the right like in the instructor's code. What gives? I even selected the same iPhone to test and it still does not work. Here is the code, tell me what I am doing wrong.

//
//  ContentView.swift
//  WeSplit
//
//  Created by Jonathan Loving on 5/23/23.
//

import SwiftUI

struct ContentView: View
{
    let students = ["Harry", "Hermione", "Ron"]
    @State private var selectedStudent = "Harry"

    var body: some View
    {
        NavigationView
        {
            Form
            {
                Picker("Select your student", selection: $selectedStudent)
                {
                    ForEach(students, id: \.self)
                    {
                        Text($0)
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

2      

Hi! SwiftUI is constantly changing so default styles are changing with that. Just add PickerStyle modifier as navigationLink and it will do the same.

Form {
    Picker("Select your student", selection: $selectedStudent) {
        ForEach(students, id: \.self) {
            Text($0)
        }
    }
    .pickerStyle(.navigationLink)
}

just try others to see all options avaible for yourself.

2      

@Jonathan didn't pick the right option!

What gives?
When I click on select a student it gives me a drop down of all the students
it does not go to the navigation view with the back button.

Evolution

You've witnessed the constant evolution of SwiftUI! It's changing at a breath-taking pace. Buckle up! The Apple World Wide Developer's Conference is next month, and there will surely be big changes. You're in the right place to learn. @twoStraws stays up late into the night to create Playgrounds demonstrating the newest cutting edge features in the language.

Picker Styles

What you see in the video (published ages ago in October 2021!) was the default behaviour of a Picker component in SwiftUI. You'd wrap the Picker in a NavigationView and when you tapped the names, SwiftUI would navigate to a second screen displaying your options, and an optional back button. When you tapped a name, you'd return to the starting screen.

Later, SwiftUI was updated allowing you to select from several pickerStyles! Nice! The code snip below shows several of the available styles. Uncomment them one at a time to see their effect on the picker. Please return here and tell us which one you prefer and why!

Picker("Select your student", selection: $selectedStudent)
        {
            ForEach(students, id: \.self)
            {
                Text($0)
            }
        }
        // Picker objects have several optional styles!
        // Uncomment one at a time and try it out.
        // .pickerStyle(.inline)
        // .pickerStyle(.segmented)
        // .pickerStyle(.menu)
        .pickerStyle(.navigationLink)  // <-- old default picker style

Keep coding!

4      

Later, SwiftUI was updated allowing you to select from several pickerStyles! Nice! The code snip below shows several of the available styles. Uncomment them one at a time to see their effect on the picker. Please return here and tell us which one you prefer and why!

I like the .inline pickerstyle the best. It lays out all the options in neat rows and no additional navigation to go back.

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.