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