[英]Passing Data Through Segue (swift 2)
This is a Tip Calculator Project and It must have a settings view where I select the default tip rate. 这是一个小费计算器项目,并且必须具有一个设置视图,在该视图中可以选择默认的小费率。 I have some issues with passing data, when I select a default tip percentage it doesn't change in the View Controller, also I want to make the app remember the default rate when I close the app and reopened.
我在传递数据时遇到一些问题,当我选择默认的小费百分比时,它在View Controller中不会更改,我还想让应用记住关闭应用程序并重新打开时的默认费率。 I will really appreciate that some one corrects my code and test it.
我真的很感谢有人纠正我的代码并对其进行测试。 This is for entering a Computer Science Program in college, I don't have previous experience with any programming language before.
这是为了进入大学的计算机科学课程,我以前没有任何编程语言的经验。
100 TipPercentageLabel.text = "(tipDisplay)%" } } 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 {
}
}
}
}
}
SETTINGS VIEW CONTROLLER 设置视图控制器
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
Replace you DefaultRate IBAction with this: 将此替换为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()
}
And set the event as "Value Changed" as shown in the image below 并将事件设置为“ Value Changed”,如下图所示
I strongly recommend you go through a few introductory iOS & Swift tutorials online. 我强烈建议您在线阅读一些iOS和Swift入门教程。 SO is not the place to get beginner level understanding of programming topics.
SO并不是让初学者对编程主题有所了解的地方。 Some recommended tutorials:
一些推荐的教程:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.