Relative Content

Tag Archive for swiftswiftui

Upon loading a view, how do I set the value of a property based on the value of an @AppStorage property without triggering onChange?

I have a Toggle component in my view that I want to set the value for when the view loads (based on the value of an @AppStorage property). But I also have an onChange method listening to the Toggle property that I don’t want to trigger until after the view loads. I haven’t been able to get onChange to ignore that initial property set. I’ve tried using the onAppear with flags, as well as using onInit() and accessing UserDefaults directly (which is always empy when onInit() fires). What am I doing wrong here?

Unable to access the elements of a binding array in a view

I have a
Reminders structure defined as below. I have a main view where I have defined this structure using a state property in a main view, which then calls 2 other views. I have defined this state as binding property in a view. This view reads the array and then displays the values.

Unable to access the elements of a binding array in a view

I have a
Reminders structure defined as below. I have a main view where I have defined this structure using a state property in a main view, which then calls 2 other views. I have defined this state as binding property in a view. This view reads the array and then displays the values.

I add SwiftUI in TableViewCell, but sometimes TapGesture doesn’t call back

// // RCChatMessageCell.swift // Runner // // Created by chenrui on 2024/6/13. // import Foundation import SwiftUI class RCBaseMessageCell: UITableViewCell { fileprivate func createSwiftUIContent(_ parentViewModel: RCConversationViewModel,_ viewModel: RCMessageViewModel) { self.selectionStyle = .none let customCotentView = MessageFactory.createMessage(type: viewModel.message.type, viewModel: viewModel) let hostingController = UIHostingController(rootView: customCotentView) hostingController.view.translatesAutoresizingMaskIntoConstraints = false self.hostingController = hostingController self.contentView.addSubview(hostingController.view) NSLayoutConstraint.activate([ hostingController.view.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor), hostingController.view.trailingAnchor.constraint(equalTo: […]

No exact matches in call to initializer error in TextField initializer

Was trying to put together a View as in show below but getting a No exact matches in call to initializer error on the TextField line. I suspect it’s due to the format argument but couldn’t figure out how to fix it. The Xcode hints aren’t very helpful either (see the image below). and clicking on Show shows nothing.

Issue with “OK” button not closing alert in Swift

I have an alert in Swift that is supposed to close when the “OK” button is clicked. Unfortunately, this doesn’t happen. Visually, I can see in the app that the “OK” button is clickable once, but nothing happens afterward. Additionally, the “OK” button becomes unclickable, and the debug message “OK clicked” is never displayed.

Issue with “OK” button not closing alert in Swift

I have an alert in Swift that is supposed to close when the “OK” button is clicked. Unfortunately, this doesn’t happen. Visually, I can see in the app that the “OK” button is clickable once, but nothing happens afterward. Additionally, the “OK” button becomes unclickable, and the debug message “OK clicked” is never displayed.

Issue with “OK” button not closing alert in Swift

I have an alert in Swift that is supposed to close when the “OK” button is clicked. Unfortunately, this doesn’t happen. Visually, I can see in the app that the “OK” button is clickable once, but nothing happens afterward. Additionally, the “OK” button becomes unclickable, and the debug message “OK clicked” is never displayed.

Issue with “OK” button not closing alert in Swift

I have an alert in Swift that is supposed to close when the “OK” button is clicked. Unfortunately, this doesn’t happen. Visually, I can see in the app that the “OK” button is clickable once, but nothing happens afterward. Additionally, the “OK” button becomes unclickable, and the debug message “OK clicked” is never displayed.