1

Hey guys I need some help here with my code, please take a look to the images to see what I see. I'm making a Tip Calculator Project in Swift and I must have a settings view where I select the default tip rate. I have some errors and I must fix that as soon as posible. I will really appreciate that some one corrects my code and test it. Below is the code of the two ViewControllers, I did not post the image of the Settings View Controller because the website does not let me post more than two links until I get more reputation.

The error in Xcode: MainViewController


Storyboard: MainStoryBoard Check the images I have some errors at the first lines.

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()
            }
        }

}

The SettingsViewController below:

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

}

/
user229044
  • 232,980
  • 40
  • 330
  • 338
  • so, what you are trying to do is to get the selectec tip from the SettingsView to the "main" view. Right? – adolfosrs Dec 10 '15 at 13:41
  • In order to conform to the `SettingDelegate` protocol, `ViewController` has to implement the method `tipPercentageChanged`. – vacawama Dec 10 '15 at 13:45
  • 1
    You should either use a Navigation Controller so that you get the back button automatically, or you should use an *unwind segue*. Performing a regular segue to return to your main ViewController creates a new instance of it instead of returning to the original one. You'll run out of memory and your app with crash if you do this repeatedly. – vacawama Dec 10 '15 at 14:01
  • Yes @adolfosrs thats what Im trying to do –  Dec 10 '15 at 22:20
  • What are your error? – Saqib Omer Dec 17 '15 at 14:46

1 Answers1

0

For your first error:

When a viewController conforms to a protocol, it needs to implements the methods the protocol implements. In your case, define you should have

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

The first error should go away.

It looks like you are presenting the SettingsViewController using a modal transition, so you can use

self.dismissViewControllerAnimated(true, completion: {})
Laurent Rivard
  • 509
  • 4
  • 13