How can I use List in SwiftUI? - list

import SwiftUI
struct upper {
#State var data = \["aaa","bbb","ccc"\]
}
struct ContentView: View {
var body: some View {
List(upper, id: .self) { item in
Text(item)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I am the beginner .I don't know how to solve the bug

This is how you could use a list:
import SwiftUI
struct ContentView: View {
var data = ["aaa","bbb","ccc"]
var body: some View {
List {
ForEach(0..<data.count) { index in
Text(data[index])
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I would really recommend you to learn the basics of SwiftUI: https://developer.apple.com/tutorials/swiftui

Related

How To Add GeometryProxy To PreviewProvider

Okay so I am passing in GeometryProxy into a struct view body and I have no idea how to do this and still use PreviewProvider can someone please help me with this?
struct MyView: View {
let geometry: GeometryProxy
var body: some View {
VStack {
.... More Code Here
}
}
}
struct MyView_Previews: PreviewProvider {
static var previews: some View {
Group {
// This is where Im having trouble. I have no idea how to pass in GeometryProxy
MyView()
}
}
It should like this:
struct MyView_Previews: PreviewProvider {
static var previews: some View {
GeometryReader { proxy in
MyView(proxy: proxy)
}
}
}
In that case you don't need to pass the geometry proxy as they'll have the same geometry. That should be enough:
struct MyView: View {
var body: some View {
GeometryProxy { proxy in
VStack {
.... More Code Here
}
}
}
}
But, if you really want to pass the geometry, you can do this:
struct MyView_Previews: PreviewProvider {
static var previews: some View {
GeometryProxy {
MyView(proxy: proxy)
}
}
}

How can I use a common variable in a Tab VIew?

I'm currently developing an application using SwiftUI.
This app has 3 structs
①ContentView
②FirstView
③SecondView
These 3 structs do page transition in Tab View.
And this app has a common variable type of Bool using ObservableObject annotation.
I want to change to appear and disappear Text View in the FirstView and the SecondView depends on the condition of the variable, but the FirstView doesn't change a view as I expected...
How can I solve this situation?
Here are the codes:
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
FirstView()
.tabItem {
Text("First")
}.tag(1)
SecondView()
.tabItem {
Text("Second")
}.tag(2)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
FirstView.swift
import SwiftUI
struct FirstView: View {
#ObservedObject var firstCheck: ViewModel = ViewModel()
var body: some View {
VStack{
if firstCheck.check == true{
Text("checked")
}
}
}
}
struct FirstView_Previews: PreviewProvider {
static var previews: some View {
FirstView()
}
}
SecondView.swift
import SwiftUI
struct SecondView: View {
#ObservedObject var secondCheck = ViewModel()
var body: some View {
VStack{
Toggle(
isOn: $secondCheck.check
){
Text("change")
}
if self.secondCheck.check == true{
Text("checked")
}
}
}
}
struct SecondView_Previews: PreviewProvider {
static var previews: some View {
SecondView()
}
}
ViewModel.swift
import Foundation
final class ViewModel: ObservableObject {
#Published var check: Bool = false
}
Xcode: Version 11.7
Keep object in one place, can be parent view
struct ContentView: View {
#ObservedObject var viewModel = ViewModel()
// #StateObject var viewModel = ViewModel() // SwiftUI 2.0
var body: some View {
TabView {
// .. other code here
}
.environmentObject(viewModel) // << inject here
}
}
and then use in both views like (for second the same)
struct FirstView: View {
#EnvironmentObject var firstCheck: ViewModel // declare only
// will be injected by type
var body: some View {
VStack{
if firstCheck.check == true{
Text("checked")
}
}
}
}

How to get a value as to which view is selected in TabView?

I'm currently developing an application using SwiftUI.
This app has 2 Views controlled a Tab View.
I want to get a value as to which view is selected in TabView.
Is there any way to do that?
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
FirstView()
.tabItem {
Text("First")
}.tag(1)
SecondView()
.tabItem {
Text("Second")
}.tag(2)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
FirstView.swift
import SwiftUI
struct FirstView: View {
var body: some View {
Text("FirstView")
}
}
struct FirstView_Previews: PreviewProvider {
static var previews: some View {
FirstView()
}
}
SecondView.swift
import SwiftUI
struct SecondView: View {
var body: some View {
Text("SecondView")
}
}
struct SecondView_Previews: PreviewProvider {
static var previews: some View {
SecondView()
}
}
Xcode: Version 11.7
Swift: Swift 5
You need to use selection, like below
Note: selection should be same type as used for tags (and use corresponding values from tags to select specific tab programmatically)
struct ContentView: View {
#State private var selectedTab = 1 // default selection
var body: some View {
TabView(selection: $selectedTab) { // << here !!
FirstView()
.tabItem {
Text("First")
}.tag(1)
SecondView()
.tabItem {
Text("Second")
}.tag(2)
}
}
}

The App protocol not work with the View's PreviewProvider

The following code can be work in ios13.
import SwiftUI
struct ContentView: View {
var body: some View {
LoginView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
But when I try to use the ios14 feature app protocol, the preview report require the ContentView confirm to View error.
import SwiftUI
#main
struct ContentView: App {
var body: some Scene {
WindowGroup {
LoginView()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
This is because in your new version the ContentView is no longer declared as a View but as an App (I'd suggest to rename it to ContentApp to avoid confusion).
And the signature of previews is:
static var previews: some View
which means it needs to return some View (and not some App).
In your case you probably want to present LoginView in previews:
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
}
}
or if you want to present ContentView:
#main
struct ContentApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
LoginView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Swiftui: I can show the data from db in list ,but it shown nothing in picker, how can I fix it

Swiftui: I can show the data from db in list ,but it shown nothing in picker, how can I fix it
here is my ContentView:
import SwiftUI
struct ContentView: View {
#ObservedObject var model = PostListViewModel()
#State private var selectedStrength = 0
var body: some View {
// List(model.posts) { post in
// Text(post.name)
// }
Picker(selection: $selectedStrength, label: Text("picker")) {
ForEach(model.posts) { post in
Text(post.name)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
try this or something similar:
ForEach(model.posts, id: \.self) { post in
Text(post.name)
}