TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Trying to set the TabView selection for TabViewStyle of paged

Forums > SwiftUI

I am having an issue with trying to figure out how to set the selected page on a TabView. As you can see from the code below, I have a view that will show all of my fetched results from CoreData. Inside my TabView I am iterating over each returned item and creating an HStack that contains an Image and a Title. This works great and I can swipe and view each connection, as well as when it is tapped, it selectes a connection. When it is selected the user is shown a view that allows them to Login to that connection, and when logged in, it will save the connections UUID stored in the Connection.ID field. The onAppear() below will get that UUID string and convert it back to a UUID and store that into my pageIndex. My pageIndex never changes when I swipe and change, as well as setting the selected to the last selected connection. What am I doing wrong?

struct AppConnectionsView: View {
    @Binding var selected_connection : Connection?
    @FetchRequest(sortDescriptors: [SortDescriptor(\.connectionDesc)]) private var connectionItems : FetchedResults<Connection>
    @State private var pageIndex = UUID()

    var body: some View {

        TabView(selection: $pageIndex) {

            ForEach(connectionItems, id: \.id) { item in
                HStack {
                    VStack{

                        PT_Utilities.getImageFromData(imageFromBase64Data: item.connectionImage)
                            .resizable()
                            .scaledToFit()
                            .padding(2)
                            .frame(width: 150, height: 150)
                            .shadow(radius: 10)
                            .onTapGesture {
                                selected_connection = item
                            }

                        Text(item.connectionDesc ?? "")
                            .font(.largeTitle)
                        Text(item.id?.uuidString ?? "")
                            .font(.caption2)
                    }
                    .frame(width: 450.0, height: 250.0)
                }
                .padding(25)
                .tag(item.id)
            }

        }
        .tabViewStyle(.page(indexDisplayMode: .always))
        .indexViewStyle(.page(backgroundDisplayMode: .always))
        .onChange(of: pageIndex) { print("Page Index Changed To: \($0)") }
        .onAppear(){
            let uuidString = PT_Utilities.getLastConnection()
            if let uuid = UUID(uuidString: uuidString)
            {
                pageIndex = uuid
                print("Last ID: \(pageIndex.uuidString)")
            }
        }
    }
}

2      

I figured it out... and I hope it is the correct way to do it, but it works.

struct AppConnectionsView: View {
    @Binding var selected_connection : Connection?
    @FetchRequest(sortDescriptors: [SortDescriptor(\.connectionDesc)]) private var connectionItems : FetchedResults<Connection>
    @State private var pageIndex = 0

    var body: some View {

        TabView(selection: $pageIndex) {

            ForEach(connectionItems.indices, id: \.self) { index in
                HStack {
                    VStack{

                        PT_Utilities.getImageFromData(imageFromBase64Data: connectionItems[index].connectionImage)
                            .resizable()
                            .scaledToFit()
                            .padding(2)
                            .frame(width: 150, height: 150)
                            .shadow(radius: 10)
                            .onTapGesture {
                                selected_connection = connectionItems[index]
                            }

                        Text(connectionItems[index].connectionDesc ?? "")
                            .font(.largeTitle)
                    }
                    .frame(width: 450.0, height: 250.0)
                }
                .padding(25)
                .tag(index)
            }

        }
        .tabViewStyle(.page(indexDisplayMode: .always))
        .indexViewStyle(.page(backgroundDisplayMode: .always))
        .onChange(of: pageIndex) { print("Page Index Changed To: \($0)") }
        .onAppear(){
            let uuidString = PT_Utilities.getLastConnection()
            if let index = connectionItems.firstIndex(where: {$0.id?.uuidString == uuidString} )
            {
                pageIndex = index
            }
            print("Last ID: \(pageIndex)")
        }

    }
}

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

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

Reply to this topic…

You need to create an account or log in to reply.

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.