Post

Replies

Boosts

Views

Activity

Reply to SwiftUI:how to show data from DB with picker
thank you for your answer,I have to show the data from my db,I get the data shown on List,when I get the data shown on pickerI get the the error:Thread 1: Fatal error: Index out of rangeit seems like i get nothing from users[]do you know how to modify it?I will really appreciate it if you can show me how to fill a foreach in picker to show all the data from users[]here is my code:ContentView.swift:import SwiftUI struct SwiftUIView_Main: View { var body: some View { ContentViewpicker() // ListView() } } struct ContentViewpicker: View { var body: some View { let users = UserList().users return Picker(selection: /*@START_MENU_TOKEN@*/.constant(1)/*@END_MENU_TOKEN@*/, label: /*@START_MENU_TOKEN@*/Text("Picker")/*@END_MENU_TOKEN@*/) { Text(users[0].name).tag(1) } } } struct ListView: View { @ObservedObject var store = UserList() var body: some View { List (store.users,id: \.id) { (user) in UserRow(user: user) } } } struct UserRow: View { var user: User var body: some View { HStack { Text(String(user.id)) Text(user.name) } } } struct SwiftUIView_Main_Previews: PreviewProvider { static var previews: some View { SwiftUIView_Main().previewLayout(.fixed(width: 1194, height: 834)) } }UserList.swift:import Foundation import Combine class UserList: ObservableObject { @Published var users: [User] = [] init() { load() } func load() { let url = URL(string: "http://localhost:3000/user")! URLSession.shared.dataTask(with: url) { data, response, error in DispatchQueue.main.async { self.users = try! JSONDecoder().decode([User].self, from: data!) } }.resume() } } //struct User: Decodable, Identifiable { struct User: Decodable { var id: Int var name: String }
Jan ’20
Reply to SwiftUI:how to get the secondView showed in full screen when I transfer to secondView from firstView
thank you for your code ,it worked!I want to ask another question: I transfered the view from the FirstView to SecondView, from SecondView to ThirdView.How to transferthe view from ThirdView to FirstView?here is my code:import SwiftUI struct ContentView: View { @State var showSecondView:Bool = false var body: some View { ZStack{ Button(action: { self.showSecondView.toggle() }) { Text("showSecondView") } if self.showSecondView { SecondView(showSecondView: self.$showSecondView) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.red) .animation(.easeInOut) .transition(.move(edge: .bottom)) } } } } struct SecondView: View { @Binding var showSecondView:Bool @State var showThirdView:Bool = false var body: some View { ZStack{ Button(action: { self.showThirdView.toggle() }) { Text("showThirdView") } if self.showThirdView { ThirdView(showThirdView: self.$showThirdView) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.yellow) .animation(.easeInOut) .transition(.move(edge: .bottom)) } } } } struct ThirdView: View { @Binding var showThirdView:Bool var body: some View { ZStack{ Button(action: { }) { Text("showFirstView") } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Jan ’20