I am new to coding, Swift and Xcode. I am in the process of creating an application which will multiply 3 values (length, width, height) to calculate the volume of a specific object.
The end goal of the app is to be able to calculate the volume of many objects and having a "total" value which will accumulatively add up the volumes for you. Getting a start on my app I have a layout sorted which includes the three text fields where the user will punch in the values (length, width, height) and a text which has has "Total =". I have successfully made the text fields accept doubles by using:
TextField("length", value: $length, formatter: NumberFormatter())
I am using a button to execute the multiplication using:
Button(action: {self.equation = self.length * self.width * self.thickness})
And a Text
where the value will show:
Text("Total= \(equation)")
Unfortunately, when I run the app, insert values into each of the text fields and hit the button the "Total=" Text
does not execute the action of the button. Just trying to figure out where I have gone wrong.
TextField
with formatter does not apply input string till explicit commit, so try the following
extension UIApplication { // just helper extension
static func endEditing() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, for:nil)
}
}
and
Button(action: {
UIApplication.endEditing() // << here !!
self.equation = self.length * self.width * self.thickness
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.