大家好,我的代码在这里需要帮助,请查看图片以查看所见内容,我在Swift中制作了一个小费计算器项目,并且必须具有一个设置视图,在该视图中我选择了默认小费率,我有一些错误,我必须尽快解决。我真的很感谢有人纠正我的代码并对其进行测试。
图1(MainViewController)图2(MainStoryBoard
下面是两个ViewController的代码,我没有发布Settings View Controller的图像,因为在获得更多声誉之前,网站不允许我发布两个以上的链接。

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 = newValue
            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()

    }

最佳答案

首先,您尚未在ViewController类中实现tipPercentageChanged方法。由于tipPercentageChanged是SettingDelegate协议的必需方法,因此必须实现此方法以符合协议。

第二,在prepareForSegue中

        SettingsViewController.delegate = newValue


实际上应该是

        SettingsViewController.delegate = self


由于SettingsViewController的委托,将是ViewController。

因此,您在ViewController中更新的代码应为

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let SettingsViewController = segue.destinationViewController as?SettingsViewController {
        SettingsViewController.delegate = self
        refreshCalculation()
    }
}

func tipPercentageChanged(newValue: Double) {

    // What you want to do, when Percentage is changed
    // Update tipSlider
    tipSlider.value = Float(newValue)
}

09-30 11:56