I apologize in advance, as I'm very new to Swift programming and Xcode, but I'm trying to build an application for my Senior Project in school. My issue stems from being able to use data from a picker view to then assign a value to a variable, based on what was selected in the picker view. For example, when "Aluminum 80" is selected in picker view, I want it to assign 0.399 to the FV variable. I'm not sure how to accomplish this, and I've tried multiple different methods, but nothing seems to work. I thought it would work directly from "data" and using my if...else structure, but it's just defaulting to the 0 that I initialized FV too earlier.
I'd also like to apologize if my code looks sloppy, as I'm still learning. My segment button is still a work in progress. Everything else works as intended though. Thanks in advance
//
// ScubaViewController.swift
// Navy Diving Air Calculations
//
// Created by Tyler Ault on 5/15/21.
//
import UIKit
class ScubaViewController: UIViewController{
let consumptionScuba:Double = 1.4
var allowedTime:Double = 0
var consumptionAir:Double = 0
var volumeAvailable:Double = 0
var mmP:Double = 250
var FV:Double = 0.0
var cylN:Double = 1
let data = ["Aluminum 100", "Aluminum 80", "Aluminum 63", "Aluminum 50", "Steel 120", "Steel 100", "Steel 72"]
override func viewDidLoad() {
super.viewDidLoad()
picker.dataSource = self
picker.delegate = self
}
@IBOutlet var picker: UIPickerView!
@IBOutlet var plannedDepth: UITextField!
@IBOutlet var cylinderPSI: UITextField!
@IBOutlet var timeOutput: UILabel!
@IBAction func didChangeSegment (_ sender: UISegmentedControl){
if sender.selectedSegmentIndex == 0 {
cylN = 1
}
else if sender.selectedSegmentIndex == 1 {
cylN = 2
}
}
@IBAction func btnCalcClicked(){ //The code below this is to perform the necessary air calculations. The if...else structure assigns the appropriate floodable volume dependant on the air cylinder type and size selected from the picker, and then assigns the value from the string to FV. The calculations below this plug in all other necessary values
if data == ["Aluminum 100"]{
FV = 0.47
}else if data == ["Aluminum 80"]{
FV = 0.399
}else if data == ["Aluminum 63"]{
FV = 0.319
}else if data == ["Aluminum 50"]{
FV = 0.281
}else if data == ["Steel 120"]{
FV = 0.526
}else if data == ["Steel 100"]{
FV = 0.445
}else if data == ["Steel 72"]{
FV = 0.42
}
consumptionAir = Double(((Double(plannedDepth.text!)! + 33)/33)) * 1.4
volumeAvailable = Double((Double(cylinderPSI.text!)! - mmP) / 14.7) * FV * cylN
allowedTime = volumeAvailable / consumptionAir
timeOutput.text = String(allowedTime) //Outputs the time allowed for the dive, in minutes, based on all factors input.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ScubaViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) - Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) - Int {
return data.count
}
}
extension ScubaViewController: UIPickerViewDelegate{
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent: Int) - String? {
return data[row]
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hello again. My app calculates as expected in every manner except that when I change the picker options, it internalizes the user input and doesn't allow the user to change inputs anymore. I was thinking about including a reset button, but for a more ideal UI experience, is there a way that after I have the calculate button pressed and it outputs my time, can I have the user selected picker options and input data reset to default values?
Again, I'm new to Swift, so I apologize for basic questions like this, and help is greatly appreciated.
//
// ScubaViewController.swift
// Navy Diving Air Calculations
//
// Created by Tyler Ault on 5/15/21.
//
import UIKit
class ScubaViewController: UIViewController{
let consumptionScuba:Double = 1.4
var allowedTime:Double = 0
var consumptionAir:Double = 0
var volumeAvailable:Double = 0
var mmP:Double = 250
var fv:Double = 0.0
var cylN:Int = 1
let data = [
(title: "Aluminum 100", fv: 0.47 ),
(title: "Aluminum 80", fv: 0.399),
(title: "Aluminum 63", fv: 0.319),
(title: "Aluminum 50", fv: 0.281),
(title: "Steel 120", fv: 0.526),
(title: "Steel 100", fv: 0.445),
(title: "Steel 72", fv: 0.42)
]
let data2 = [
(title: "One Cylinder", cylN: 1 ),
(title: "Two Cylinders", cylN: 2 )
]
override func viewDidLoad() {
super.viewDidLoad()
picker.dataSource = self
picker.delegate = self
pickerCyl.dataSource = self
pickerCyl.delegate = self
picker.tag = 1
pickerCyl.tag = 2
}
@IBOutlet var picker: UIPickerView!
@IBOutlet var plannedDepth: UITextField!
@IBOutlet var cylinderPSI: UITextField!
@IBOutlet var timeOutput: UILabel!
@IBOutlet var pickerCyl: UIPickerView!
@IBAction func btnCalcClicked(){ //The code below this is to perform the necessary air calculations.
let selectedRow = picker.selectedRow(inComponent: 0)
guard data.indices.contains(selectedRow) else {
print("selectedRow invalid")
return
}
fv = data[selectedRow].fv
let selectedRow2 = pickerCyl.selectedRow(inComponent: 0)
guard data2.indices.contains(selectedRow) else {
print("selectedRow invalid")
return
}
cylN = data2[selectedRow2].cylN
let depthText = plannedDepth.text ?? "0" //Handle nil textfield
let value = depthText == "" ? 0.0 : (Double(depthText) ?? 0.0)
consumptionAir = ((value + 33) / 33) * 1.4
let psiText = cylinderPSI.text ?? "0"
let value2 = psiText == "" ? 0.0 : (Double(psiText) ?? 0.0)
volumeAvailable = ((value2 - mmP) / 14.7) * fv * Double(cylN)
allowedTime = volumeAvailable / consumptionAir
timeOutput.text = String(allowedTime.truncate(places:0)) + " Minutes"
//Outputs the time allowed for the dive, in minutes, based on all factors input.
}
override func touchesBegan(_ touches: SetUITouch, with event: UIEvent?) {
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ScubaViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) - Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) - Int {
switch pickerView.tag {
case 1:
return data.count
case 2:
return data2.count
default:
return 1
}
}
}
extension ScubaViewController: UIPickerViewDelegate{
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent: Int) - String? {
switch pickerView.tag {
case 1:
return data[row].title
case 2:
return data2[row].title
default:
return "Data not found"
}
}
}
extension Double {
func truncate(places : Int)- Double {
return Double(floor(pow(10.0, Double(places)) * self)/pow(10.0, Double(places)))
} //This function is designed to allow me truncate the time output. As a safety factor, times are always rounded down, so an easier method was to just truncate the decimal off of "allowedTime"
}