Swift3在viewControllers之间共享了一个类实例

时间:2017-03-12 03:37:04

标签: ios swift3

我是iOS编程的新手,我正在尝试构建一个简单的计算器应用程序,但我仍然坚持在两个viewControllers之间连接动作。我试图通过在第二个视图中操作相同的控件来更改firstView的segmentControl的值。有什么建议怎么办?谢谢。以下是它的代码和截图。

代码第一个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")
}

代码第二个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) {


}
}

屏幕

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您应该使用UserDefaults来连接数据,因为它会保存下次打开应用的提示默认值。

在第二个视图控制器中

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

在第一个视图控制器中

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

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

请记住注册默认值

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

AppDelegate.swift

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

    NSUserDefaults.standardUserDefaults().registerDefaults([
    "DefaultTip": <your default value>
])
    return true
}
相关问题