Sheet View struct crashes due to "unresolved identifier" error - identifier

First Stack Overflow Question
I'm new to iOS Development. This is my second app in the class and I've followed along the tutorial and I cannot figure out what is wrong here.
I created a new SwiftUI file and named it "NewTaskView" and now I'm trying to use the struct "NewTaskView in a sheet view.
Why is Xcode saying "Use of unresolved identifier 'NewTaskView', when it has been created but in a separate SwiftUI file?
Here are my code snippets and a screenshot of the error
NewTaskView.swift
import SwiftUI
struct NewTaskView: View {
#State var text = ""
var body: some View {
TextField("Task Name", text: $text)
}
}
struct NewTaskView_Previews: PreviewProvider {
static var previews: some View {
NewTaskView()
}
}
ContentView.swift
import SwiftUI
struct ContentView: View {
var taskStore: TaskStore
#State var modalIsPresented = false
var body: some View {
NavigationView {
List(taskStore.tasks) { task in
Text(task.name)
}
.navigationBarTitle("Tasks")
.navigationBarItems(
trailing:
Button(action: {
self.modalIsPresented = true
}) {
Image(systemName: "plus")
}
)
}.sheet(isPresented: $modalIsPresented) {
NewTaskView()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(taskStore: TaskStore() )
}
}
Screenshot of Code with Error
Use of unresolved identifier 'NewTaskView' error

Answer:
Bug in xcode 11.7 works fine in xcode 12

Related

cannot find $exercisePlan in scope (SwiftUI) [duplicate]

How do I generate a preview provider for a view which has a binding property?
struct AddContainer: View {
#Binding var isShowingAddContainer: Bool
var body: some View {
Button(action: {
self.isShowingAddContainer = false
}) {
Text("Pop")
}
}
}
struct AddContainer_Previews: PreviewProvider {
static var previews: some View {
// ERROR HERE <<<-----
AddContainer(isShowingAddContainer: Binding<Bool>()
}
}
In Code above, How to pass a Binding<Bool> property in an initialiser of a view?
Just create a local static var, mark it as #State and pass it as a Binding $
struct AddContainer_Previews: PreviewProvider {
#State static var isShowing = false
static var previews: some View {
AddContainer(isShowingAddContainer: $isShowing)
}
}
If you want to watch the binding:
Both other solutions [the "static var" variant AND the "constant(.false)"-variant work for just seeing a preview that is static. But you cannot not see/watch the changes of the value intended by the button action, because you get only a static preview with this solutions.
If you want to really watch (in the life preview) this changing, you could easily implement a nested view within the PreviewProvider, to - let's say - simulate the binding over two places (in one preview).
import SwiftUI
struct BoolButtonView: View {
#Binding var boolValue : Bool
var body: some View {
VStack {
Text("The boolValue in BoolButtonView = \(boolValue.string)")
.multilineTextAlignment(.center)
.padding()
Button("Toggle me") {
boolValue.toggle()
}
.padding()
}
}
}
struct BoolButtonView_Previews: PreviewProvider {
// we show the simulated view, not the BoolButtonView itself
static var previews: some View {
OtherView()
.preferredColorScheme(.dark)
}
// nested OTHER VIEW providing the one value for binding makes the trick
struct OtherView : View {
#State var providedValue : Bool = false
var body: some View {
BoolButtonView(boolValue: $providedValue)
}
}
}
Other way
struct AddContainer_Previews: PreviewProvider {
static var previews: some View {
AddContainer(isShowingAddContainer: .constant(false))
}
}

SwiftUI How to use .refreshable view modifier without a list?

iOS 15 introduces the '.refreshable' View Modifier, but most of the examples I've seen use a List. I want to implement a pull to refresh feature in an app that does not use a list but just a VStack with Text views like the below. How can I implement this so that pull down to refresh will refresh my data?
import SwiftUI
struct ContentView: View {
#State var songName = "Song"
#State var artistName = "Artist"
var body: some View {
VStack {
Text(songName)
Text(artistName)
}
.refreshable {
reloadData()
}
}
private func reloadData() {
songName = "Let it Be"
artistName = "The Beatles"
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Swift UI updating #EnvironmentObject prevent other update on #StateObject

I have the following view ModalView opened by by a parent view ParentView using a button in the toolbar
** Parent View **
import SwiftUI
struct ParentView: View {
#EnvironmentObject var environmentObject: MainStore
#State private var showCreationView = false
var body: some View {
NavigationView {
Text("ENV_OBJ Count: \(environmentObject.shoppingChartFullList.count)")
.navigationBarTitle(Text("Navigation Bar Title"))
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button(action: { showCreationView = true }) {
Image(systemName: "plus")
}
.sheet(isPresented: $showCreationView, content: {
ModalView(showModelView: $showCreationView)
})
}
}
}
}
}
struct ParentView_Previews: PreviewProvider {
static var previews: some View {
ParentView().environmentObject(MainStore())
}
}
** Modal View **
import SwiftUI
struct ModalView: View {
#EnvironmentObject var environmentObject: MainStore
#Binding var showModelView: Bool
#State var newItem = ShoppingChartModel()
var body: some View {
Text(/*#START_MENU_TOKEN#*/"Hello, World!"/*#END_MENU_TOKEN#*/)
Button(action: {
environmentObject.shoppingChartFullList.append(newItem)
self.showModelView = false
}) {Text("Salva")}
}
}
struct ModalView_Previews: PreviewProvider {
static var previews: some View {
ModalView(showModelView: .constant(true)).environmentObject(MainStore())
}
}
** Main Store **
import Foundation
import SwiftUI
import Combine
final class MainStore: ObservableObject {
//An observable object needs to publish any changes to its data, so that its subscribers can pick up the change.
#Published var shoppingChartFullList: [ShoppingChartModel] = load()
}
The problem is that, action executed by the Save Button in the Modal View doesn't dismiss the modal (even if it correctly updates both the Boolean variable and the Env_Obj).
Seems to be related with the toolbar... in fact if I remove the Navigation View and the toolbar, putting the button directly in a stack ... it works...
In the Parent2View I remove the NavigationView and just configured the button directly in the view after the Text property. And it is working as expected.
import SwiftUI
struct ParentView2: View {
#EnvironmentObject var environmentObject: MainStore
#State private var showCreationView = false
var body: some View {
VStack {
Text("ENV_OBJ Count: \(environmentObject.shoppingChartFullList.count)")
Button(action: { showCreationView = true }) {
Image(systemName: "plus")
}
.sheet(isPresented: $showCreationView, content: {
ModalView(showModelView: $showCreationView)
})
}
}
}
struct ParentView2_Previews: PreviewProvider {
static var previews: some View {
ParentView2().environmentObject(MainStore())
}
}
UPDATE
Ok I finally found the solution. The issue is the toolbar. If the ParentView is modified as follow, all start working well
struct ParentView: View {
#EnvironmentObject var environmentObject: MainStore
#State private var showCreationView = false
var body: some View {
NavigationView {
Text("ENV_OBJ Count: \(environmentObject.shoppingChartFullList.count)")
.navigationBarTitle("Nav View Title")
.navigationBarItems(trailing:
Button(action: {
self.showCreationView.toggle()
})
{
Image(systemName: "plus")
.font(Font.system(.title))
}
)
}
.sheet(isPresented: $showCreationView) {
ModalView(showModelView: $showCreationView)
}
}
}
Thanks!
Cristian

NavigationLink in SwiftUI not working anymore

I´m creating an App and use NavigationLink in Swift/SwiftUI, but it doesn't work anymore. I don't now since when, but 2 or 3 weeks ago, all working fine. The NavigationLinks which already are in the code for longer, working fine. But today I've used new ones, and they don´t work. It looks in the Simulator and on a real device, if they are disabled or something. They are grey and you can't click on them or if you clicked on them, nothing happens. Is there any solution?
import SwiftUI
struct MedikamenteView: View {
var body: some View {
Form {
NavigationLink(
destination: ASSView(),
label: {
Text("ASS")
})
NavigationLink(
destination: AdrenalinView(),
label: {
Text("Adrenalin")
})
}
}
}
struct MedikamenteView_Previews: PreviewProvider {
static var previews: some View {
MedikamenteView()
}
}
And for example, this one is working fine:
import SwiftUI
struct RechtView: View {
var body: some View {
Form {
NavigationLink(
destination: ParagraphenView(),
label: {
Text("Paragraphen")
})
NavigationLink(
destination: AufklaerungEinwilligungView(),
label: {
Text("Die Aufklärung mit nachfolgender Einwilligung")
})
NavigationLink(
destination: NotSanGView(),
label: {
Text("Wichtiges aus dem NotSanG")
})
}
.navigationTitle("Recht")
}
}
struct RechtView_Previews: PreviewProvider {
static var previews: some View {
RechtView()
}
}
You have to use NavigationLinks inside NavigationView{}.
Without it NavigationLink wont work.
Try this:
import SwiftUI
struct MedikamenteView: View {
var body: some View {
NavigationView {
Form {
NavigationLink(
destination: ASSView(),
label: {
Text("ASS")
})
NavigationLink(
destination: AdrenalinView(),
label: {
Text("Adrenalin")
})
}
}
}
}
struct MedikamenteView_Previews: PreviewProvider {
static var previews: some View {
MedikamenteView()
}
}
Your second code sample might be loaded from previous view which has used NavigationView {}
I can see from the comments that you've found out it will only work in a NavigationView and are now wondering why. It only matters that your view is embedded in a NavigationView directly above it the View hierarchy. For example, this code would work:
struct FirstView: View {
var body: some View {
NavigationView {
NavigationLink(label: "Go to next view", destination: NextView())
}
}
}
struct NextView: View {
...
While this won't:
struct FirstView: View {
#State var modalPresented = false
var body: some View {
NavigationView {
Button("Show fullscreen cover"){
modalPresented = true
}
}
.fullScreenCover(isPresented: $modalPresented, content: SecondView())
}
}
struct SecondView: View {
var body: some View {
NavigationLink(label: "Go to next view", destination: NextView())
// Doesn't work because the view is in a fullScreenCover and therefore not a part of the NavigationView.
}
}

How do I update a text label in SwiftUI?

I have a SwiftUI text label and i want to write something in it after I press a button.
Here is my code:
Button(action: {
registerRequest() // here is where the variable message changes its value
}) {
Text("SignUp")
}
Text(message) // this is the label that I want to change
How do I do this?
With only the code you shared it is hard to say exactly how you should do it but here are a couple good ways:
The first way is to put the string in a #State variable so that it can be mutated and any change to it will cause an update to the view. Here is an example that you can test with Live Previews:
import SwiftUI
struct UpdateTextView: View {
#State var textToUpdate = "Update me!"
var body: some View {
VStack {
Button(action: {
self.textToUpdate = "I've been udpated!"
}) {
Text("SignUp")
}
Text(textToUpdate)
}
}
}
struct UpdateTextView_Previews: PreviewProvider {
static var previews: some View {
UpdateTextView()
}
}
If your string is stored in a class that is external to the view you can use implement the ObservableObject protocol on your class and make the string variable #Published so that any change to it will cause an update to the view. In the view you need to make your class variable an #ObservedObject to finish hooking it all up. Here is an example you can play with in Live Previews:
import SwiftUI
class ExternalModel: ObservableObject {
#Published var textToUpdate: String = "Update me!"
func registerRequest() {
// other functionality
textToUpdate = "I've been updated!"
}
}
struct UpdateTextViewExternal: View {
#ObservedObject var viewModel: ExternalModel
var body: some View {
VStack {
Button(action: {
self.viewModel.registerRequest()
}) {
Text("SignUp")
}
Text(self.viewModel.textToUpdate)
}
}
}
struct UpdateTextViewExternal_Previews: PreviewProvider {
static var previews: some View {
UpdateTextViewExternal(viewModel: ExternalModel())
}
}

Resources