-2

Hi I'm trying to pass data from two different classes and two different storyboards. I want to pass a UIimage and UILabel from sweetAndSalty4ViewController to cartViewController when the user clicks "sendData" button.

This is my code:

sweetAndSalty4ViewController which I want to pass data from:

import UIKit     
    
class sweetAndSalty4ViewController: UIViewController {
    @IBOutlet var imageViewSweet: UIImageView!
    @IBOutlet var labelsweet: UILabel!
    @IBOutlet var sweeetTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func sendData(_ sender: UIButton) {
        // I want to pass both labelSweet and imageViewSweet to cartViewController  
    }
}

And this is cartViewController in which I want to display the UIimage and the UILabel:

import UIKit

class cartViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var cartItem = ""
    @IBOutlet var cartTable: UITableView!
    
    override func viewDidLoad() {
        cartTable.delegate = self
        cartTable.dataSource = self
        //  foodTable.separatorStyle = .none
        view.backgroundColor = UIColor.init(red: 228/255, green: 230/255, blue: 234/255, alpha: 1)
        navigationItem.title = "My Cart"
        
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cartItem.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        240
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell") as! cartTableViewCell
        cell.layer.cornerRadius = 30
        cell.layer.borderWidth = 15.0
        cell.layer.borderColor = UIColor.white.cgColor
        cell.cartImage.layer.masksToBounds = true
        cell.cartImage.layer.cornerRadius = 2
        cell.cartImage.translatesAutoresizingMaskIntoConstraints = false
        cell.cartImage.contentMode = .scaleAspectFill
        cell.cartLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        cell.cartLabel.textColor = .black
        cell.cartLabel.translatesAutoresizingMaskIntoConstraints = false
        cell.cartLabel.backgroundColor = .systemGray5
        cell.cartLabel.text = ""
        cell.cartImage.image = UIImage(named: "")
        //  cell.contentView.backgroundColor = .white
        return cell
    }
}

Please help me because I'm trying with it for weeks.

kRiZ
  • 2,320
  • 4
  • 28
  • 39
faisal
  • 1
  • Does this answer your question? [Sending data with Segue with Swift](https://stackoverflow.com/questions/26089152/sending-data-with-segue-with-swift) – RodolfoAntonici Aug 04 '20 at 02:32

1 Answers1

0

maybe you should look this tutorial from TheCodex on Youtube or going to Apple's tutorials about segues

RodolfoAntonici
  • 1,555
  • 21
  • 34
  • i already go through it. i didn't get what i want , i want to pass it without segue using delegate or notificationCenter so, how can i pass both of them using this method. – faisal Aug 04 '20 at 18:29
  • How you're presenting the `cartViewController`? Via storyboard? Via instance? NotificationCenter shouldn't be used to send data between view controllers because it's a system wide propagation of data, which is not good. Using delegate is overcomplicated to send data forward, since you have one more object to deal with memory management – RodolfoAntonici Aug 05 '20 at 19:47
  • ok what is the solution ? please help me because i still searching about it for weeks – faisal Aug 05 '20 at 19:59
  • I cannot answer that without knowing the way you present the next screen – RodolfoAntonici Aug 10 '20 at 23:54