in my app I am storing a variable with different finger measurements. The different finger measurements have values in the UITableViewCell but they come up as nil in the SwiftUI View.
In the UITableViewCell, I made the variable a published variable. I then went to the SwiftUI View and tried @ObservedObject and then tried @StateObject when @ObservedObject didn't work. Again, logs in the UITableViewCell show values but logging in the SwiftUI View come up as nil and default to 0.
Below are snippets of code of the published var as well as the type. Then a snippet of the SwiftUI View where i try to pass the variable.
struct SaveNailImagesResponse: Codable {
var id: Int?
var leftIndexLength: Double?
var leftIndexSize: Int?
var leftIndexWidth: Double?
var leftMiddleLength: Double?
var leftMiddleSize: Int?
var leftMiddleWidth: Double?
var leftPinkyLength: Double?
var leftPinkySize: Int?
var leftPinkyWidth:
}
/// SNIPPET OF UITABLEVIEWCELL
class HandMeasurementsTableViewCell: UITableViewCell, ObservableObject {
@Published var fingerSizeData: SaveNailImagesResponse? {
didSet {
self.tableView.reloadData()
print(self.fingerSizeData?.leftIndexSize as Any)
print(self.fingerSizeData?.leftRingSize as Any)
print(self.fingerSizeData?.leftMiddleSize as Any)
}
}
}
/// HERE IS SNIPPET OF SWIFTUI VIEW: I tried passing it a 2 different ways below but comes up as nil and defaults to 0 every time
struct ResultsThumbnailView1: View {
@Environment(\.presentationMode) var presentationMode
@ObservedObject var data: HandMeasurementsTableViewCell = HandMeasurementsTableViewCell()
var body: some View {
VStack {
Text(String(self.data.fingerSizeData?.leftRingSize ?? 0))
Text("Size \(String(data.fingerSizeData?.leftIndexSize ?? 0))")
.font(.system(size: 19, weight: .bold))
}
}
}
// VIEW CONTROLLER WHERE EVERYTHING CONNECTS
class ResultsSizesViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var resultsData: SaveNailImagesResponse?
private var thumbnailButton: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(systemName: "camera.circle"), for: .normal)
button.tintColor = .black
button.addTarget(self, action: #selector(presentThumbnailPage), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
["HandMeasurementsTableViewCell"].forEach( {
tableView.register(UINib.init(nibName: $0, bundle: nil), forCellReuseIdentifier: $0)
})
tableView.delegate = self
tableView.dataSource = self
setNavBar(backButtonAvailable: true, title: "Results")
setUI()
thumbnailButtonConstraints()
}
@objc func presentThumbnailPage() {
let vc = UIHostingController(rootView: ResultsThumbnailTabView())
vc.modalPresentationStyle = .fullScreen
self.navigationController?.present(vc, animated: true)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HandMeasurementsTableViewCell") as? HandMeasurementsTableViewCell
let data = self.resultsData
cell?.fingerSizeData = data
if indexPath.row == 0 {
cell?.titleLabel.text = "Left Hand"
} else if indexPath.row == 1 {
cell?.titleLabel.text = "Right Hand"
}
return cell!
}
func thumbnailButtonConstraints() {
view.addSubview(thumbnailButton)
thumbnailButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
thumbnailButton.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 20),
thumbnailButton.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
thumbnailButton.heightAnchor.constraint(equalToConstant: 20),
thumbnailButton.widthAnchor.constraint(equalToConstant: 20),
])
thumbnailButton.addTarget(self, action: #selector(presentThumbnailPage), for: .touchUpInside)
}
}
CodePudding user response:
struct ResultsThumbnailView: View {
let resultsData: SaveNailImagesResponse
let vc = UIHostingController(rootView: ResultsThumbnailView(resultsData: resultsData))