Swiftui Running on Simulator/Preview: Toggle Not Working, Console Logs: "Invalid Mode 'Kcfrunloopcommonmodes'"

SwiftUI - usage of toggles - console logs: “invalid mode 'kCFRunLoopCommonModes'” - didSet does not work

  1. Ignore that warning, it's SwiftUI internals and does not affect anything. If you'd like submit feedback to Apple.

  2. didSet does not work, because self here (as View struct) is immutable, and @State is just property wrapper which via non-mutating setter stores wrapped value outside of self.

Update: do something on toggle

@State private var notifyCheck = false

var body: some View {

let bindingOn = Binding<Bool> (
get: { self.notifyCheck },
set: { newValue in
self.notifyCheck = newValue
// << do anything
}
)
return Toggle(isOn: bindingOn) {
Text("Activate?")
}
}

What does invalid mode 'kCFRunLoopCommonModes' ...mean?

I think it's a warning that Apple should fix it itself. Even in this sample project which is from WWDC19 this problem exists. There is a UISwitch in a cell of a table. When I tap it, the mentioned warning is raised.

So, in my view, it is a bug that Apple should deal with.

SwiftUI's new NavigationStack does not navigate to next screen

I just want to navigate from OnBoardingView to UserTypeView when user presses a button in OnBoardingView,
then try this approach, where .navigationDestination(...) is moved to the OnBoardingView,
as shown in this example code:

@main
struct CealUIApp: App {

@State var path = [String]()

var body: some Scene {
WindowGroup {
NavigationStack(path: $path) {
OnBoardingView(path: $path)
}
}
}
}

struct OnBoardingView: View {
@Binding var path: [String]

var body: some View {
Button {
path.append("UserTypeView")
} label: {
Text("go to UserTypeView")
}
.navigationDestination(for: String.self) { string in
UserTypeView()
}
}
}

struct UserTypeView: View {
var body: some View {
Text("UserTypeView")
}
}

Performing actions / working with gestures via Live Preview (SwiftUI)

You can define a struct for this purpose:

struct StatefulPreviewWrapper<Value, Content: View>: View {
@State var value: Value
var content: (Binding<Value>) -> Content

var body: some View {
content($value)
}

init(_ value: Value, content: @escaping (Binding<Value>) -> Content) {
self._value = State(wrappedValue: value)
self.content = content
}
}

Then use it in your preview like this:


struct CustomToggle_Previews: PreviewProvider {
static var previews: some View {
StatefulPreviewWrapper(false) { CustomToggle(isOn: $0) }
}
}

Ternary operation not executing when toggling Bool value

This is happening because your condition is always true, rather than the current true/false state of your x variable.

x = true ? false : true

^^^^

As written, this code will always assign false to x since the condition won't ever fail. When written as a normal if statement, the code you've attempted to use would look like this

if true {
x = false
} else {
x = true
}

Which makes it easier to see why the else clause is never entered. Instead, you should be using the following.

x = x ? false : true // x = x == true ? false : true

Which assigns x to false if x is true and x to true if x is false.



Related Topics



Leave a reply



Submit