1

i’m newbie in swift language and i want to pass the price from the Item view controller to other controller (Payment view controller) using array. Can anyone help me? Thank you

Here the code for the item detail view controller

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)

    }


}

And here the code for the payment

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

}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
New user
  • 147
  • 4
  • 18

2 Answers2

0

you can use this code ... try to use first letter small for objects

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
     }

    }
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
0

If you want to unwrap: DestViewController.price = labelprice?.text ?? “”

This way is not recommended but you can use DestViewController.price = labelprice!.text

Search for unwarp in Swift

Bac Tran
  • 3
  • 4