Hi, I'm in the process of updating my dilution calculator app to be fully done in SwiftUI. I'm trying to rewrite the old part of UIKit to SwiftUI. Can I get some help on how to do this?
Here is the code form UIKit:
@IBAction func button(_ sender: Any) {
self.ContainerSizeTextField.resignFirstResponder()
self.DilutionRatioTextField.resignFirstResponder()
let firstValue = Double(ContainerSizeTextField.text ?? "0") ?? 0.0
let secondValue = Double(DilutionRatioTextField.text ?? "0") ?? 0.0
let thirdValue = Double(1)
let outputValue = Double(secondValue + thirdValue)
var outputValue1: Double = 0
if outputValue > 0.00000001 || outputValue < -0.0000000001 {
outputValue1 = Double(firstValue / outputValue)
}
let outputValue2 = Double(firstValue - outputValue1)
let c:String = String(format:"%.1f", outputValue1)
let d:String = String(format:"%.1f", outputValue2)
TotalProductLabel.text = " \(c)"
TotalWaterLabel.text = " \(d)"
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi, I'm working on a Dilution calculator and I get the error "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"
It was running fine but then I decided to add a "clear button" and that's when the error started.
@IBAction func button(_ sender: Any) {
self.ContainerSizeTextField.resignFirstResponder()
self.DilutionRatioTextField.resignFirstResponder()
let firstValue = Double(ContainerSizeTextField.text!)
let secondValue = Double(DilutionRatioTextField.text!)
let thirdValue = Double(1)
let outputValue = Double(secondValue! + thirdValue)
let outputValue1 = Double(firstValue! / outputValue)
let outputValue2 = Double(firstValue! - outputValue1)
let c:String = String(format:"%.1f", outputValue1)
let d:String = String(format:"%.1f", outputValue2)
TotalProductLabel.text = " \(c)"
TotalWaterLabel.text = " \(d)"
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
@IBAction func clear(_ sender: Any) {
ContainerSizeTextField.text = " "
DilutionRatioTextField.text = " "
TotalProductLabel.text = "0"
TotalWaterLabel.text = "0"
Error happens at "let outputValue = Double(secondValue! + thirdValue)"
I'm able to run the app once before I get the error. Usually after hitting the "clear button".
Hi,
I’m working on a dilution calculator. I have it 98% working, however, I want it to work a certain way and I’m not sure to do that. This is my first app so I’m new at this.
So I want the user to be able to input the numbers and hit a button to get the calculation. I’ve been using @State and through my research and understanding, using that instantly updates any changes the user makes.
So how do I go about making the app wait till the user hits the “Go” button.
Hers my code so far.
@State private var ContainerSize = 0
@State private var DilutionRatio = 0
@State private var Go = ""
@State private var TotalProduct = 0.0
@State private var TotalWater = 0.0
@FocusState private var amountIsFocused: Bool
var totalProductAmount: Double {
let firstValue = Double(ContainerSize)
let secondValue = Double(DilutionRatio + 1)
let totalProduct = Double(firstValue / secondValue)
return totalProduct
}
var totalWaterAmount: Double {
let firstValue = Double(ContainerSize)
let secondValue = Double(DilutionRatio + 1)
let totalWater = Double(firstValue - secondValue)
return totalWater
}
//Container Size
ZStack {
Image("Container Size (Oz)")
.padding(.vertical, -15)
TextField("", value: $ContainerSize, format: .number)
.frame(width: 200.0, height: 60.0)
.multilineTextAlignment(.center)
.font(Font.system(size: 50, design: .default))
.keyboardType(.decimalPad)
.focused($amountIsFocused)
}
}
//Dilution Ratio
ZStack {
Image("Dilution Ratio - 2")
.padding(.vertical, -10)
TextField("", value: $DilutionRatio, format: .number)
.frame(width: 200.0, height: 60.0)
.multilineTextAlignment(.center)
.font(Font.system(size: 50, design: .default))
.keyboardType(.decimalPad)
.focused($amountIsFocused)
}
//Go Button
Button(action: {}, label: {
Image("Go Button")
})
//Results
HStack{
ZStack {
Image("Total Product (Oz)")
Text("\(totalProductAmount, specifier: "%.1f")")
.font(Font.system(size: 60, design: .default))
}
ZStack {
Image("Total Water (Oz)")
Text(totalWaterAmount, format: .number)
.font(Font.system(size: 60, design: .default))
}
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer(
Button("Done") {
amountIsFocused = false
}
}
struct CalculatorIView_Previews: PreviewProvider {
static var previews: some View {
CalculatorIView()
}
}
The calculator works as is but I want the user to input numbers, hit the “Go” button, and the results are shown.
I'm reletivly new to SwiftUI, so this might be a simple solution. I would like to have a Background Image behind a NavigatinLink but I can't seem to get the image to show. I looked everywhere on how to do this. I was able to briefly get it working using GeometryReader. However, when Xcode 14 was released it no longer was working and now I'm back to square one. I can't seem to find solution. Any help would be great, Thanks.
Before Xcode Update:
After Xcode Update (Background Missing)
UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UITableView.appearance().backgroundColor = .clear
UITableViewCell.appearance().backgroundColor = .black
UITableView.appearance().tableFooterView = UIView() }
var categories: [Category] = CategoryList.Ten
var body: some View {
NavigationView {
VStack {
VStack(alignment: .leading, spacing: -15) {
List(categories, id: \.id) { category in
NavigationLink(destination: CategoriesDetailView(category: category), label: {
Text(category.title)
.fontWeight(.light)
.lineLimit(2)
.listRowBackground(Color.black)
.minimumScaleFactor(0.5)
.frame(maxWidth: 1000, minHeight: 0, alignment: .topLeading)
.padding(.vertical, 0)
}
)
.listRowBackground(Color.black)
.listRowSeparatorTint(.white)
.foregroundColor(.white)
}
ZStack {
VStack {}
.navigationTitle("Categories")
.foregroundColor(.white)
}
}
.background(
GeometryReader { geo in
Image("Catagories-1")
.resizable()
.aspectRatio(geo.size, contentMode: .fill)
.edgesIgnoringSafeArea(.all)
.padding(.vertical, 10)
}
)
}
}.accentColor(.white)
}
struct CategoriesListView_Previews: PreviewProvider {
static var previews: some View {
CategoriesListView()
}
}
}
Hey everyone, I'm relatively new to SwiftUI, so this might be a simple solution. I’m working on a dilution calculator and it works with no issues. However, there is always a “0” over the placeholder in the textfield for Container Size and Dilution Ratio. I don't mind the "0", I actually want a "0" there. But I have to erase it every time I tap on the textfield to input a number. Even if I remove the placeholder it's still there. How do I make it so that I don't have to keep erasing the "0" every time I want to input a number but keep the placeholder. Thanks.
Example:
[https://i.makeagif.com/media/10-05-2022/-d_E8J.gif)
struct CalculatorView: View {
@State private var containerSize = 0
@State private var dilutionRatio = 0
@State private var totalProduct = 0.0
@State private var totalWater = 0.0
@FocusState private var amountIsFocused: Bool
@FocusState private var focusedInput: Field?
func totalProductAmount() -> Double {
let firstValue = Double(containerSize)
let secondValue = Double(dilutionRatio + 1)
let totalProduct = Double(firstValue / secondValue)
return totalProduct
}
func totalWaterAmount() -> Double {
let firstValue = Double(containerSize)
let secondValue = Double(dilutionRatio + 1)
let totalProduct = Double(firstValue / secondValue)
let totalWater = Double(firstValue - totalProduct)
return totalWater
}
var body: some View {
NavigationView {
VStack(alignment: .center) {
Image("Logo")
.padding(.horizontal, 30)
HStack {
//Container Size
ZStack {
Image("Container Size (Oz)")
.padding(.vertical, -15)
TextField("", value: $containerSize, format: .number)
.frame(width: 200.0, height: 60.0)
.multilineTextAlignment(.center)
.font(Font.system(size: 50, design: .default))
.foregroundColor(.white)
.keyboardType(.decimalPad)
.focused($amountIsFocused)
.focused($focusedInput, equals: .containerSize)
}
}
//Dilution Ratio
ZStack {
Image("Dilution Ratio - 2")
.padding(.vertical, -10)
TextField("", value: $dilutionRatio, format: .number)
.frame(width: 200.0, height: 60.0)
.multilineTextAlignment(.center)
.font(Font.system(size: 50, design: .default))
.foregroundColor(.white)
.keyboardType(.decimalPad)
.focused($amountIsFocused)
.focused($focusedInput, equals: .dilutionRatio)
}
//Go Button
Button(action: {
totalProduct = totalProductAmount()
totalWater = totalWaterAmount()
amountIsFocused = false
}, label: {
Image("Go Button")
})
//Results
HStack{
ZStack {
Image("Total Product (Oz)")
Text("\(totalProduct, specifier: "%.1f")")
.font(Font.system(size: 60, design: .default))
.foregroundColor(.white)
}
ZStack {
Image("Total Water (Oz)")
Text("\(totalWater, specifier: "%.1f")")
.font(Font.system(size: 60, design: .default))
.foregroundColor(.white)
}
```