如何将数据从视图控制器传递到其他视图控制器

我是新手,所以我想使用数组将价格从“项目”视图控制器传递给其他控制器(“付款”视图控制器)。谁能帮我?谢谢

这是项目详细信息视图控制器的代码

import UIKit


class ItemDetailViewController: UIViewController {

     var items = [item]()

    var name : String = ""
    var price : String = ""
    var imagee : String = ""

    @IBOutlet weak var labelname: UILabel!

    @IBOutlet weak var image: UIImageView!

    @IBOutlet weak var labelprice: UILabel!

// This one got error. 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        var DestViewController : PaymentViewController = segue.destination as! PaymentViewController

        DestViewController.price = labelprice?[IndexPath.text]

    }

    @IBAction func addtoPayment(_ sender: Any) {



        }



    override func viewDidLoad() {
        super.viewDidLoad()

        labelname.text = name
        labelprice.text = price
        image.image = UIImage(named: imagee)

    }


}

这是付款代码

import UIKit

class PaymentViewController: UIViewController {

     var items = [item]()
    var price : String = ""

    @IBOutlet weak var paymentdetails: UILabel!


    @IBOutlet weak var cardnametextfield: UITextField!


    @IBOutlet weak var validthrutextfield: UITextField!


    @IBOutlet weak var cardnumbertextfield: UITextField!

    @IBOutlet weak var cvcnumbertextfield: UITextField!

    @IBOutlet weak var labelprice: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    labelprice.text = price


        // Do any additional setup after loading the view.
    }


    @IBAction func paybutton(_ sender: Any) {

        if cardnametextfield.text == "" {
            alertMessage(titleInput: "Error, Payment Unsuccessful!", messageInput: "Please Fill all the fields")
        } else if validthrutextfield.text == "" {
            alertMessage(titleInput: "Error, Payment Unsuccessful!", messageInput: "Please Fill all the fields")
        } else if cardnumbertextfield.text == "" {
            alertMessage(titleInput: "Error, Payment Unsuccessful!", messageInput: "Please Fill all the fields")
        } else if cardnumbertextfield.text == "" {
        alertMessage(titleInput: "Error, Payment Unsuccessful!", messageInput: "Please Fill all the fields")
        } else {
            alertMessage(titleInput: "Success!", messageInput: "Payment Successful!")

            self.transitionToHomePage()
        }
    }

    func alertMessage(titleInput: String, messageInput: String){
        let alert = UIAlertController(title: titleInput, message: messageInput, preferredStyle: UIAlertController.Style.alert)
        let paybutton = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)

        alert.addAction(paybutton)
        self.present(alert, animated: true, completion: nil)
    }

    func transitionToHomePage(){
         let TabHomeViewController = storyboard?.instantiateViewController(identifier: Constrants.Storyboard.TabHomeViewController) as? UITabBarController

        view.window?.rootViewController = TabHomeViewController
        view.window?.makeKeyAndVisible()
    }

}
评论