准备segue,传递数据

时间:2015-12-10 13:25:46

标签: ios xcode swift segue

嘿伙计我在这里需要一些帮助我的代码,请看看图片,看看我看到了什么。我在Swift中制作了一个Tip Calculator项目,我必须有一个设置视图,我在其中选择默认的提示率。我有一些错误,我必须尽快解决。我真的很感激有人纠正我的代码并对其进行测试。下面是两个ViewControllers的代码,我没有发布设置视图控制器的图像,因为在我获得更多声誉之前,网站不允许我发布两个以上的链接。

Xcode中的错误:MainViewController

故事板:MainStoryBoard检查图像我在第一行有一些错误。

import UIKit

class ViewController: UIViewController, SettingDelegate {
// 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 : Double = NSUserDefaults.standardUserDefaults().doubleForKey("DefaultTipRate") ?? 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: UISlider) {

    tipPercentage = Double(round(tipSlider.value)) / 100
    refreshCalculation()
}


@IBAction func StepperPersonChanged(sender: UIStepper) {
    numberOfPerson = Int(round(personsStepper.value))
    refreshCalculation()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let SettingsViewController = segue.destinationViewController as?SettingsViewController {
        SettingsViewController.delegate = self
        refreshCalculation()
            }
        }

}

下面的SettingsViewController:

import UIKit
protocol SettingDelegate {
func tipPercentageChanged(newValue : Double)
}
class SettingsViewController: UIViewController {
var destName : String!
var delegate : SettingDelegate?
@IBOutlet weak var tipControl: UISegmentedControl!

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 tipRates = [0.05, 0.10, 0.15, 0.20, 0.25, 0.30]
    let tipRate = tipRates[tipControl.selectedSegmentIndex]

    delegate?.tipPercentageChanged(tipRate)

    NSUserDefaults.standardUserDefaults().setDouble(tipRate, forKey: "DefaultTipRate")
    NSUserDefaults.standardUserDefaults().synchronize()

}

/

1 个答案:

答案 0 :(得分:0)

第一次出错:

viewController符合协议时,需要实现协议实现的方法。在您的情况下,定义您应该

func tipPercentageChanged(newValue : Double) {
    //save the new value here
}

第一个错误应该消失。

您似乎正在使用SettingsViewController转换来展示modal,因此您可以使用

self.dismissViewControllerAnimated(true, completion: {})