0

I am new to iOS programming and I am trying to build a simple calculator app, but I am stuck at connecting actions between two viewControllers. I am trying to change the value of a segmentControl of the firstView by manipulating the same control in my second view. Any suggestions how to do it? Thank you. Below are the code and screenshots of it.

code first viewController

class ViewController: UIViewController {

@IBOutlet weak var billField: UITextField!
@IBOutlet weak var totalLabel: UILabel!
@IBOutlet weak var tipLabel: UILabel!
@IBOutlet weak var tipControll: UISegmentedControl!


@IBAction func onTap(_ sender: Any) {

    //dismiss the keyboard when we tap anywhere from the screen
    view.endEditing(true)
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func calculateTip(_ sender: AnyObject) {

    //calculate
    let percentage = [0.18, 0.2, 0.25]

    let bill = Double(billField.text!) ?? 0
    let tip = bill * percentage[tipControll.selectedSegmentIndex]

    let total = bill + tip

    //set the labels 

    tipLabel.text = String(format: "$%.2f", tip)
    totalLabel.text  = String(format: "$%.2f", total)

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("view will appear")
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    print("view did appear")
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    print("View will disappear")
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    print("view did disappeared")
}

code second viewController

 import UIKit

 class SettingViewController: UIViewController {


@IBOutlet weak var segmentControlLabel: 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.
}



/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/


@IBAction func onTipChanged(_ sender: Any) {


}
}

Screen

enter image description here

Celaro
  • 196
  • 1
  • 7
  • 19
  • Please refer to this question: [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – duan Mar 12 '17 at 03:40

1 Answers1

0

I think you should use UserDefaults to connect data because it will save your tip default for next time open app.

In second viewcontroller

 override func viewDidLoad() {
     super.viewDidLoad()

      // Get value saved in last time here.
      //NSUserDefaults.standardUserDefaults().integerForKey("DefaultTip")
 }

 @IBAction func onTipChanged(_ sender: Any) {

       // Set value when tip changed
       //NSUserDefaults.standardUserDefaults().setInteger(value, forKey: "DefaultTip")
 }

In first viewcontroller

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("view will appear")

    // Get value saved in last time here.
    //NSUserDefaults.standardUserDefaults().integerForKey("DefaultTip")
}

And remember register default value

NSUserDefaults.standardUserDefaults().registerDefaults([
    "DefaultTip": <your default value>
])

at AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    NSUserDefaults.standardUserDefaults().registerDefaults([
    "DefaultTip": <your default value>
])
    return true
}
Luan Tran
  • 1,142
  • 7
  • 15