通过Segue传递数据(swift 2)

时间:2015-12-13 20:09:02

标签: ios swift segue

这是一个小费计算器项目,它必须有一个设置视图,我在其中选择默认的小费率。我在传递数据方面存在一些问题,当我选择默认的提示百分比时,它在视图控制器中没有变化,我也希望在关闭应用程序并重新打开时让应用程序记住默认速率。我真的很感激有人纠正我的代码并对其进行测试。这是为了进入大学的计算机科学课程,我之前没有任何编程语言的经验。

100         TipPercentageLabel.text ="(tipDisplay)%"         }     }

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) {
    va
                if let tip = tipRates {
                    ViewController.tipPercentage = tip/100

2 个答案:

答案 0 :(得分:1)

将DefaultRate IBAction替换为:

@IBAction func changevalue(sender: UISegmentedControl) { 

    var tipRate = [5, 10, 15, 20, 25, 30]
    tipRates = Double(tipRate[tipControl.selectedSegmentIndex])   
    delegate?.tipPercentageChanged(tipRates!) print("(tipRates)")
    NSUserDefaults.standardUserDefaults().setDouble(tipRates!, forKey: "DefaultTipRate")
    NSUserDefaults.standardUserDefaults().synchronize() 
}

将事件设置为“Value Changed”,如下图所示

enter image description here

答案 1 :(得分:0)

我强烈建议您浏览一些介绍性的iOS& Swift教程在线。因此,不是初学者对编程主题的理解的地方。一些推荐的教程:

Swift 2 Tutorial

Apple swift tutorials