Saturday, November 26, 2022
HomeiOS Developmentios - Swift UI: What's the best method to create distinctive cases...

ios – Swift UI: What’s the best method to create distinctive cases of a struct that save particular person information?


I wish to construct an inventory app that’s much like “Notes” or “Reminders”. At any time when I click on a row of the listing (like in “Reminders”, while you click on on an inventory), I would really like it to take me to a newly generated view, with particular person information (the listing, with a title, and information).

I constructed a primary display that reveals a Checklist, and every row is a Navigation View to a brand new display which comprises a brand new listing. The second listing has three rows, every row an HStack with a preset string to the left, and a textfield to the proper. The textfield to the proper is editable.

Upon click on, the one distinction between separate cases of default second views needs to be the title, being a string equal to the string within the row of the primary listing.

I wish to click on on the textfield, write one thing down, and that string to stay saved to the second view.

The code I’m displaying right here doesn’t create separate cases of second view. At any time when I click on a row within the first listing, I get the identical second view, with the identical information.

So if I click on the row with “First Entry”, I’m taken to second view. I modify the “Title” textfield to “Charles”, reserve it in AppStorage, and after I exit this view and click on the “Second Row”, I’m taken to the identical iteration of second view, with “Title: Charles”.

Thanks kindly!

import SwiftUI

/// THE FIRST VIEW

struct ContentView: View {

//1. Theme Coloration

@State var themeColor: Coloration = Coloration.orange

//2. Default Entry

@State var firstListDefault: [String] = ["First Entry", "Second Entry", "Third Entry"]

//3. Empty String for Textual content Area

@State var toAddEntry = ""

// BODY

var physique: some View {
    
    //A. Checklist
    
    Checklist {
        
        //B. For every - To a second view that takes a title = to entry
        
        ForEach(firstListDefault, id: .self) {index in
            NavigationLink {
                SecondView(title: index)
            }
        
        //C. Row Label
            
        label: {
                HStack {
                    Picture(systemName: "circle.inset.crammed").foregroundColor(themeColor)
                    Textual content(index)
                }
            }
            
            //D. DELETE AND MOVE + NAVIGATION TITLE
        
        }
        .onDelete(carry out: { IndexSet in
            firstListDefault.take away(atOffsets: IndexSet)
        })
        .onMove(carry out: { IndexSet, Int in
            firstListDefault.transfer(fromOffsets: IndexSet, toOffset: Int)
        })
        .navigationTitle(Textual content("Shopper Checklist"))
        
        
        //E. NEW ENTRY TEXT FIELD
        
        TextField("New Entry", textual content: $toAddEntry)
            .autocorrectionDisabled()
            .textInputAutocapitalization(.by no means)
            .onSubmit {
            firstListDefault.append(toAddEntry)
            toAddEntry = ""
        }
    }
    
    //F. TOOLBAR
    
    
    .toolbar {
        ToolbarItem(placement: .navigationBarTrailing) {
            EditButton().foregroundColor(themeColor)
        }
    }
    
}    
}

/// THE SECOND VIEW

struct SecondView: View {

@AppStorage("identify") var inputText1 = ""
@AppStorage("file") var inputText2 = ""
@AppStorage("due") var inputText3 = ""

let title: String

var physique: some View {
    Checklist {
        HStack {
            Textual content("Shopper Title:")
            
            TextField("...", textual content: $inputText1).autocorrectionDisabled()
                .textInputAutocapitalization(.by no means)
                .multilineTextAlignment(.trailing)
        }.navigationTitle(Textual content(title))
        
        HStack {
            Textual content("File Quantity:")
            Spacer()
            TextField("...", textual content: $inputText2).autocorrectionDisabled()
                .textInputAutocapitalization(.by no means)
                .multilineTextAlignment(.trailing)
        }
        
        HStack {
            Textual content("Due date:")
            Spacer()
            TextField("...", textual content: $inputText3).autocorrectionDisabled()
                .textInputAutocapitalization(.by no means)
                .multilineTextAlignment(.trailing)
        }
        
    }
}
}


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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments