嘿伙计我需要一个人帮我完成我的应用程序,我需要在12月15日之前完成它。我在Swift2中制作一个Tip Calculator项目,它必须有一个设置视图,我选择默认的提示率。我在传递数据方面存在一些问题,当我选择默认提示百分比时,它在视图控制器中没有变化,我也希望在关闭应用程序并重新打开时让应用程序记住默认速率。我真的很感激有人纠正我的代码并对其进行测试。我是新手,下面是两个ViewControllers的代码和Main.Storyboard的截图(Image 1)(ViewController Screenshot) 我为我糟糕的英语道歉,不是我的母语
的ViewController
import UIKit
class ViewController: UIViewController {
//Inputs
@IBOutlet weak var amountTextField: UITextField!
//Labels
@IBOutlet weak var TipPercentageLabel: UILabel!
@IBOutlet weak var numberOfPersonLabel: UILabel!
@IBOutlet weak var tipAmountLabel: UILabel!
@IBOutlet weak var totalBillLabel: UILabel!
@IBOutlet weak var billPerPersonLabel: UILabel!
//Slider & Stepper
@IBOutlet weak var tipSlider: UISlider!
@IBOutlet weak var personsStepper: UIStepper!
//Variables
var tipPercentage = 0.20
var numberOfPerson:Int = 1
let numberFormatter:NSNumberFormatter = NSNumberFormatter()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tipAmountLabel.text = "$0.00"
totalBillLabel.text = "Bill Total"
billPerPersonLabel.text = "$0.00"
TipPercentageLabel.text = "20.0%"
numberOfPersonLabel.text = "1"
self.amountTextField.becomeFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupContainer() {
tipSlider.minimumValue = 0
tipSlider.maximumValue = 100
tipSlider.value = 20
tipSlider.addTarget(self, action: "sliderTipChanged:", forControlEvents: .ValueChanged)
personsStepper.minimumValue = 1
personsStepper.maximumValue = 30
personsStepper.value = 1
personsStepper.addTarget(self, action: "sliderPersonChanged:", forControlEvents: .ValueChanged)
amountTextField.text = ""
refreshCalculation()
}
@IBAction func OnEditingFieldBill(sender: AnyObject) {
refreshCalculation()
}
func refreshCalculation() {
numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
if let amount = numberFormatter.numberFromString(amountTextField.text!) as? Double {
let tipAmount = amount * tipPercentage
let totalBill = amount + tipAmount
let billPerPerson = totalBill / Double(numberOfPerson)
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
tipAmountLabel.text = numberFormatter.stringFromNumber(tipAmount)
totalBillLabel.text = numberFormatter.stringFromNumber(totalBill)
billPerPersonLabel.text = numberFormatter.stringFromNumber(billPerPerson)
} else {
tipAmountLabel.text = "-"
totalBillLabel.text = "-"
billPerPersonLabel.text = "-"
}
numberFormatter.numberStyle = NSNumberFormatterStyle.PercentStyle
numberFormatter.minimumFractionDigits = 1
numberFormatter.maximumFractionDigits = 1
TipPercentageLabel.text = self.numberFormatter.stringFromNumber(tipPercentage)
numberOfPersonLabel.text = "\(numberOfPerson)"
}
@IBAction func sliderTipChanged(sender: AnyObject) {
tipPercentage = Double(round(tipSlider.value)) / 100
refreshCalculation()
}
@IBAction func StepperPersonChanged(sender: AnyObject) {
numberOfPerson = Int(round(personsStepper.value))
refreshCalculation()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let id = segue.identifier {
if id == "show settings" {
if let SettingsViewController = segue.destinationViewController as? SettingsViewController {
}
}
}
}
}
设置视图控制器
import UIKit
class SettingsViewController: UIViewController {
@IBOutlet weak var tipControl: UISegmentedControl!
var tipRates:Double?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func DefaultRate(sender: AnyObject) {
var tipRate = [5, 10, 15, 20, 25, 30]
var tipRates = Double(tipRate[tipControl.selectedSegmentIndex])
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let id = segue.identifier {
if id == "goBackToViewController" {
if let ViewController = segue.destinationViewController as? ViewController {
if let tip = tipRates {
ViewController.tipPercentage = tip/100
}
}
}
}
答案 0 :(得分:1)
---- 从评论中编辑 ----
我认为它没有按照您的意愿进行更新的原因是由于此行的一个小错误。
var tipRates = Double(tipRate[tipControl.selectedSegmentIndex])
UISegmentedControl的DefaultRate动作函数内部
使用var是对同一变量名称的重新声明,因此您尝试在prepareForSegue中传递的是一个空变量。
此功能应更改为:
@IBAction func DefaultRate(sender: AnyObject) {
var tipRate = [5, 10, 15, 20, 25, 30]
tipRates = Double(tipRate[tipControl.selectedSegmentIndex])}
希望这会解决错误。
---- 结束编辑 ----
从viewController的viewDidLoad函数中我可以看到,您正在设置tip标签,而不是根据变量tip tipPercentage更新值。
TipPercentageLabel.text = "20.0%"
将值显示设置为始终为20.0%,您可以在此处使用。
var tipDisplay = tipPercentage * 100
TipPercentageLabel.text = "\(tipDisplay)%"
这应该更新标签中显示的值,但是你从不打电话给你的其他功能重新计算金额等。
因此你也应该呼吁
func setupContainer()
或
func refreshCalculation()
在ViewDidLoad()中。
关于在应用关闭时记住默认值,您应该考虑使用 NSUserDefaults 。
Some information regarding NSUserDefaults can be found here,它解释了实施少量保存的数据,并且可以非常简单地在您的情况下实施。