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

时间:2020-05-23 14:44:45

标签: ios arrays swift xcode

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

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

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

}

2 个答案:

答案 0 :(得分:0)

您可以使用此代码...尝试对对象使用小的首字母

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

        if let lText = labelprice?.text {
          destViewController.price = lText
     }

    }

答案 1 :(得分:0)

如果要展开: DestViewController.price = labelprice?.text ?? “”

不推荐这种方式,但是您可以使用 DestViewController.price = labelprice!.text

在Swift中搜索不变形