Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Swift" how to put different View into a List?

I new come to Xcode,

here is my code

struct ContentView: View {
var body: some View {
        NavigationView {
            List {
                TestView().padding();
                TestView2().padding();
                TestView3().padding();
                TestView4().padding();
                  }
            .navigationBarTitle(Text("Place"))
            .navigationBarItems(trailing: EditButton())
              }
    }
}

one of the Testview:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

struct TestView: View {
@Environment(\.editMode) var mode

@State var timeNow = ""
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var dateFormatter: DateFormatter {
    let fmtr = DateFormatter()
    fmtr.dateFormat = "LLLL dd, HH:mm"
    fmtr.timeZone = TimeZone(identifier:  "Asia/Hong_Kong")!
    return fmtr
}

var body: some View {
   
    Text("HK: " + timeNow)
        .onReceive(timer) { _ in
            self.timeNow = dateFormatter.string(from: Date())
        }

}}

My preview

I want to "Press the Edit button, then I can delete different place’s clock"

I read the tutorial on the internet,if want to delete "something", it should put "something" into the "List.

However, in Xcode, "View" is not variable or int or string, how can I put the different view into "List" and call it out or delete.

Or any other way to accomplish my purpose?

My final goal is: to press the "edit Button", and choose to delete a different place.

Future, i want to add another function, press "+ button" then can choose the different place and add to display.

>Solution :

var body: some View {
    @State var locations: [String] = ["Asia/Hong_Kong"] // you can add additional location strings
    @State var isEditMode: EditMode = .inactive
    NavigationView {
        List {
            ForEach(locations, id: \.self) { location in
                TestView(location: location)
            }
        }
        .navigationBarTitle(Text("Place"))
        .navigationBarItems(trailing: EditButton())
        .environment(\.editMode, self.$isEditMode)
    }
}

struct TestView: View {
    let location: String
    @State var timeNow = ""
    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    var dateFormatter: DateFormatter {
        let fmtr = DateFormatter()
        fmtr.dateFormat = "LLLL dd, HH:mm"
        fmtr.timeZone = TimeZone(identifier:  location)!
        return fmtr
    }
    var body: some View {
        Text("HK: " + timeNow)
            .onReceive(timer) { _ in
                self.timeNow = dateFormatter.string(from: Date())
            }
    }
}

This will be work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading