Home > database >  How to parse text from each cell to another View Controller UITextField
How to parse text from each cell to another View Controller UITextField

Time:06-08

I have a class which is called InvoicesViewController "first image"

in that class there are cells that come from the API and each cell has a Label which represents a price of an item and a button near that price which segues you to PayViewController "second image"

and now i want that the UITextField in the PayViewController "second image" to be filled with the selected cell's label price.

I hope I am clear and someone guides me to the appropriate answer since i can't wrap my head around this one :)

When user clicks the button Pay a .popover is introduced

This popover's UITextField needs to be filled with the selected cell's price label

This is my Code of the tableView :

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

    let data = notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices
        
    return data?.count ?? 0
}


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "invoicesCell", for: indexPath) as? invoicesModel else { return UITableViewCell() }
    
    let currentNotifications = notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices
    let currentInvoices = currentNotifications![indexPath.row]
    

    cell.mainPriceLabelInvoices.text = "€\(currentInvoices.priceWithVAT ?? 0.00)"

    return cell
}

CodePudding user response:

you can use navigation code for move to the next page and pass the value with navigation.

let storyBoard : UIStoryboard = UIStoryboard(name: "PayViewController", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "PayViewController") as! PayViewController
nextViewController.txtfield.text = value
self.present(nextViewController, animated:true, completion:nil)

CodePudding user response:

At first, create a var in PayViewController like this

var price : Double = 0.0

Now in viewDidLoad() method write down the below code

self.textField.text = "€\(price)"

then, you have to call didSelect method of tableView in InvoicesViewController and write down the code mentioned below

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    let vc = self.storyBoard?.instantiateViewController(withIdentifier: "PayViewController") as! AddCategoriesVC
    
    let currentNotifications = notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices
    let currentInvoices = currentNotifications![indexPath.row]
    
    // This is to transfer the selcted Data to Payment page
    vc.price = currentInvoices.priceWithVAT ?? 0.00
    self.present(vc, animated:true, completion:nil)
    
}
  • Related