I want to be able to show an error popup ( Alert ) in all my views whenever some error condition occurs.
On the "logic" side this can be done by implementing a ViewModel base class containing:
#Published var showError: Bool = false
#Published var errorMessage: String = ""
Any subclass can then set those values if an error occured.
The problem however is on the View side. I would need to add the following code to every View that can show an alert:
.alert(isPresented: $viewModel.showError) {
return Alert(title: Text("error"), message: Text(viewModel.errorMessage), dismissButton: .default(Text("ok")))
}
Is there a way to not have to copy paste this everywhere? Otherwise if I want to change the Alert behaviour I would need to adapt it all over my codebase.
Related
I have a VM that is implemented as follows:
LoginViewModel
class LoginViewModel: ObservableObject {
var username: String = ""
var password: String = ""
}
In my ContentView, I use the VM as shown below:
#StateObject private var loginVM = LoginViewModel()
var body: some View {
NavigationView {
Form {
TextField("User name", text: $loginVM.username)
TextField("Password", text: $loginVM.password)
Every time I type something in the TextField it shows the following message in the output window:
Binding<String> action tried to update multiple times per frame.
Binding<String> action tried to update multiple times per frame.
Binding<String> action tried to update multiple times per frame.
It is a message and not an error.
If I decorate my username and password properties with #Published then the message goes away but the body is rendered each time I type in the TextField.
Any ideas what is going on and whether I should use #Published or not. I don't think I will gain anything from putting the #Published attribute since this is a one-way binding and I don't want to display anything on the view once the username changes.
If I decorate my username and password properties with #Published then the message goes away
This is the correct solution. You need to use #Published on those properties because that is how SwiftUI gets notified when the properties change.
the body is rendered each time I type in the TextField
That is fine. Your body method is not expensive to compute.
I don't think I will gain anything from putting the #Published attribute since this is a one-way binding
You cannot be sure SwiftUI will work correctly (now or in future releases) if you don't use #Published. SwiftUI expects to be notified when the value of a Binding changes, even when a built-in SwiftUI component like TextField causes the change.
For the simple case - the state is kept in the same view or in a ModelSupport class, consists of strings or other primitive types, and there's only one of each, #Published will work fine.
I got this error with a model class containing an array of structs and using a List, and every time you type inside a TextField inside a list (or every time you select an item in a list), the view gets refreshed, and the error gets triggered.
I am thus using a DelayedTextField:
struct DelayedTextField: View {
var title: String = ""
#Binding var text: String
#State private var tempText: String = ""
var body: some View {
TextField(title, text: $tempText, onEditingChanged: { editing in
if !editing {
$text.wrappedValue = tempText
}
})
.onAppear {
tempText = text
}
}
}
and the binding update error is no more.
I am using Core data and Swiftui and everything have been working fine but in this one view I have a List, ForEach that is not working.
So for testing purpose my code currently look like this:
#ObservedObject var viewModel = NewLearningWhyViewModel()
VStack {
ForEach(viewModel.whys, id: \.self) { why in
Text(why.why)
}
List {
ForEach(viewModel.whys, id: \.self) { why in
Text(why.why)
}
}
Button(action: {
viewModel.createWhy(why: "Test", count: viewModel.whys.count, learning: learn)
viewModel.fetchWhy(predicate: NSPredicate(format: "parentLearning == %#", learn))
}){
Text("Add")
.font(.title)
.foregroundColor(.white)
}
}
The problem is my List { ForEach, first time I press add button it shows the new why, second time i press the button the whole list goes away, the third time I press the button the list shows again with all 3 items.
To test the problem I added that first ForEach part and that shows the correct item at all times, so there is not a problem with the viewmodel or adding the items, the items are added and it is published from the viewmodel since that part is updated.
Does anyone have any clue why my List { ForEach only show every other time?
I have gotten this problem. I figure it out by adding objectWillChange in ViewModel, and send() it manually when your why is changed. Actually I don't know your NewLearningWhyViewModel clearly, so this is just an example, you should try it out.
class NewLearningWhyViewModel: ObservableObject {
let objectWillChange: ObservableObjectPublisher = ObservableObjectPublisher()
#Published var whys: Why = Why() {
didSet {
objectWillChange.send()
}
}
}
Ok the post from Becky Hansmeyer solved it, adding .id(UUID()) to the list solved it and it started working correctly...
because of "viewModel.whys" is set of classes.
SwiftUI does not work with classes directly.
There is 2 solutions:
make it struct instead of class + add #Published modifier inside of view
leave it as is + do it observable object and in init of your view assign into observed object.
More details here:
https://stackoverflow.com/a/62919526/4423545
I'm trying to create a navigation link in SwiftUI that logs in a user and navigates to the next screen.
I've tried using .simultaneousGesture as shown below, based on this solution. When I have it perform a simple action (e.g. print("hello")), the code works fine and navigates to the next page, but when I have it perform my authState.signUp function (which is async), it calls the function but doesn't navigate to the next page.
Is there a different way I should be approaching this?
NavigationLink(destination: NextView()) {
Text("Create account")
}
.simultaneousGesture(TapGesture().onEnded {
authState.signUp(user: user)
})
you can use a selection arg of navigationLink
add this var
#State var trigger: Bool? = nil
and use
NavigationLink(destination: NextView(), tag: true,
selection: $trigger ) {
}
when ever your other job (login) is done toggle() the trigger and navigationLink fires.
I have a date selector in a view and, once the user enters a date and saves I display a new view with a toggle. Ideally, once the users flips the toggle I'd like to be able to set a reminder using the date field already entered.
I have created an ObservableObject
import SwiftUI
import Combine
class UpdateVM: ObservableObject{
#Published var reminderDate = Date() {didSet {
print("set")
}
which I declare in the View as:
#ObservedObject var updateVM = UpdateVM()
if(self.isToggle){
updateVM.reminderDate = flossTheCat.reminderDate!
}
I get an error "Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols"
This works fine in the action area of a button press but I don't see if it's possible to react to a toggle flip - is the toggle just designed to reflect UI changes and should I implement a button instead ? Definitely having a hard time adjusting to the SwiftUI paradigm even if it makes sense overall
Thanks !
per the request - here is where it does work as I hoped it would (via a button)
trailing: Button(action: {
do {
let flossingReminders = FlossingPets.init(context: self.context)
self.flossingVM.reminderDate = self.flossingDate
if self.context.hasChanges {
try self.context.save()
}
}catch {
print(error)
}
I have two List's in a view and want to be able to determine which list currently has the focus in order to show the correct details of the selected item in the list in a details panel.
The following code never seems to get called, can anyone indicate whether there is another correct way to determine when focus changes.
struct StoreList: View {
#EnvironmentObject private var database: Database
#Binding var selectedStore: Store?
var body: some View {
List(selection: $selectedStore) {
ForEach(database.stores, id: \.self) { store in
StoreRow(store: store).tag(store)
.focusable(true, onFocusChange: { isFocused in
print("focus changed")
if isFocused {
self.database.selectedType = .store
}
})
}
}
.focusable(true, onFocusChange: { isFocused in
print("focus changed")
if isFocused {
self.database.selectedType = .store
}
})
}
}
In the meantime I will explore detecting mouse clicks on the Rows since the user would need to click on an item in the list to move the focus.
Currently I am setting the selectedType value when an item changes (i.e. $selectedStore) in the view model (database) but if the user selected the already selected item in the other list then the value does not get updated but the List and list item does get the focus - well the visual colour change indicates it has the focus.
EDIT:
I have also tried processing the onTapGesture callback which works fine except it replaces the List rows default behaviour. How can I make sure the event is passed through to the List as this might work then.
The easiest method of reacting to focus is
struct DummyView: View {
#Environment(\.isFocused) var isFocused
var body: some View {
Text("My view")
.padding()
.background(isFocused ? Color.orange : Color.black)
}
}