(Note. I posted this quesiton yesterday and could not find it today so am trying again.)
My app requires the user to enter four sets of data:
- food & drink total (i.e., 125.35)
- tax total (i.e., 12.55)
- tip preference (ie, 17)
- split preference (how many ways the check will be split, i.e., 3)
When the app is first launched the user enters her data. When she enters her tip preference this numeric data is followed by the % symbol per the NSNumberFormatter() code.
I have added a Reset button in the nav bar that the user may use to clear all of the data text fields. My problem is that after the Reset() code is executed the % symbol never appears even though the same exact code is run.
The code that prints out the tip pref (and %) is embedded in textFieldDidEndEditing(...) as follows:
func textFieldDidEndEditing(textField: UITextField) {
formatter.numberStyle = .CurrencyStyle
formatter.usesGroupingSeparator = false
formatter.locale = NSLocale.currentLocale() // This is the default
if textField == foodnDrinkTextField {
. . .
} else if textField == taxTextField {
. . .
} else if textField == tipTextField { /
tipTextField.resignFirstResponder()
tipCalc.tipPercentValue = Double((tipTextField.text! as NSString).doubleValue) /
formatter.numberStyle = .PercentStyle
formatter.maximumIntegerDigits = 3
tipTextField.textColor = UIColor.blueColor() /
tipTextField.font = UIFont.systemFontOfSize(26, weight: UIFontWeightThin)
/
tipInput = (tipCalc.tipPercentValue / 100)
/
tipTextField.text = formatter.stringFromNumber(tipInput) /
} else if textField == splitTextField {
The Reset() code is as follows:
@IBAction func reset(sender: AnyObject) {
if tipCalc.tipPrefValue > 100 {
defaults.removeObjectForKey("foodnDrinkTextField")
defaults.removeObjectForKey("taxTextField")
defaults.removeObjectForKey("splitTextField")
} else {
defaults.removeObjectForKey("foodnDrinkTextField")
defaults.removeObjectForKey("taxTextField")
defaults.removeObjectForKey("tipTextField")/
defaults.removeObjectForKey("splitTextField")
defaults.synchronize() /
}
resetFlags() /
navigationController?.navigationBarHidden = true
foodnDrinkTextField.text = "" /
hideAllLabelsnTextFields() /
}
In the last set of code you will note the 'defaults.synchronize()' call. I added that thinking that this is what I was missing but that did not help at all.
For the sake of clarity the resetFlags() code has no bearing on the NSNumberFormatter() code so is not included.
My idea for the Reset() function was to enable the user to clear the four data fields when she re-launched the app so she could enter new data. I have no idea why the % symbol will not print since the same exact code is executed after she enters her tip preference.
Any comments or suggestions will be greatly appreciated.
Thanks!