简体   繁体   English

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

[英]Swift3 shared a class instances between viewControllers

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. 我是iOS编程的新手,我试图构建一个简单的计算器应用程序,但是我被困在连接两个viewController之间的动作。 I am trying to change the value of a segmentControl of the firstView by manipulating the same control in my second view. 我试图通过在第二个视图中操纵相同的控件来更改firstView的segmentControl的值。 Any suggestions how to do it? 有什么建议怎么做吗? Thank you. 谢谢。 Below are the code and screenshots of it. 以下是其代码和屏幕截图。

code first viewController 代码优先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 代码第二个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 屏幕

在此处输入图片说明

I think you should use UserDefaults to connect data because it will save your tip default for next time open app. 我认为您应该使用UserDefaults连接数据,因为它将为下次打开的应用程序保存提示默认值。

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 在第一个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 AppDelegate.swift

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM