通过 Segue 传递数据(swift 2)
Posted
技术标签:
【中文标题】通过 Segue 传递数据(swift 2)【英文标题】:Passing Data Through Segue (swift 2) 【发布时间】:2015-12-13 20:09:01 【问题描述】:这是一个小费计算器项目,它必须有一个设置视图,我可以在其中选择默认小费率。我在传递数据时遇到了一些问题,当我选择默认小费百分比时,它不会在视图控制器中更改,而且我想让应用程序在我关闭应用程序并重新打开时记住默认费率。我将非常感谢有人更正我的代码并对其进行测试。这是为了进入大学计算机科学专业,我之前没有任何编程语言的经验。
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
【问题讨论】:
您是否检查过viewDidLoad
的调用时间与您在 ViewController
上设置 tipPercentage
的时间相比?
对不起,我没听懂你在说什么
在ViewController的viewDidLoad()方法中设置断点,在SettingsViewController prepareForSegue方法最后一行设置断点:ViewController.tipPercentage = tip/100
@RyanR 我该怎么做?对不起,我在这方面很新
【参考方案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”,如下图所示
【讨论】:
【参考方案2】:我强烈建议您在线阅读一些介绍性 ios 和 Swift 教程。 SO 不是让初学者了解编程主题的地方。一些推荐的教程:
Swift 2 Tutorial
Apple swift tutorials
【讨论】:
感谢您的推荐,我看过一些教程但我需要帮助,我需要在 12 月 15 日之前完成这个项目 @Yadiel 如果你不愿意先自学,那你就错了地方 是的,我正在努力了解这就是我想加入该计划的原因,我做了很多研究来编写我上面写的所有代码。以上是关于通过 Segue 传递数据(swift 2)的主要内容,如果未能解决你的问题,请参考以下文章
通过 segue 传递数据不能在 swift 3 中运行任何解决方案?