ios – The right way to go the info from ViewController to a different viewController by utilizing a button?


I’ve a DetailScreen viewController, by utilizing APIs I must go the info from DetailScreen to CartScreen.

My most important query is how one can go all the info which DetailScreen (All of the parameters) should the CartScreen?

I am utilizing MVVM so here is the code from reprository:

class FoodsDaoRepository {
        func insertToCart(title: String, picture: String, worth: Int, class: String, orderAmount: Int, userName: String){
        let params : Parameters = ["name":name, "image":image, "price":price, "category":category, "orderAmount":orderAmount, "userName":userName]

        AF.request("http://instance.com/meals/insertFood.php", technique: .publish, parameters: params).response {response in
            if let knowledge = response.knowledge {
                do {
                    let end result = attempt JSONDecoder().decode(CRUDResponse.self, from: knowledge)
                    print("Success: (end result.success!)")
                    print("Message: (end result.message!)")
                } catch {
                    print(error.localizedDescription)
                }
            }
        }
    }


    func getFromCart (userName: String) {
        let params: Parameters = ["userName" : userName]
        AF.request("http://instance.com/meals/getFoodsCart.php", technique: .publish, parameters: params).response {response in
            if let knowledge = response.knowledge {
                do{
                    let end result = attempt JSONDecoder().decode(CRUDResponse.self, from: knowledge)
                    print("Success: (end result.success!)")
                    print("Message: (end result.message!)")
                } catch {
                    print(error.localizedDescription)
                }
            }
        }
      }
    }

I would like insertFood API to insert the product to the cart, and getFoodsCart to return all of the merchandise which has been added to the cart.

This is entity for the Cart:

    class FoodsCart : Codable {

    var cartId : Int
    var title : String
    var picture : String
    var worth : Int
    var class : String
    var orderAmount : Int
    var userName : String
    
     init(cartId: Int, title: String, picture: String, worth: Int, class: String, orderAmount: Int, userName: String) {
        self.cartId = cartId
        self.title = title
        self.picture = picture
        self.worth = worth
        self.class = class
        self.orderAmount = orderAmount
        self.userName = userName
    }
}

Element ViewModel class :

class DetailViewModel {

var frepo = FoodsDaoRepository()

func insertToCart(title: String, picture: String, worth: Int, class: String, orderAmount: Int, userName: String) {
    frepo.insertToCart(title: title, picture: picture, worth: worth, class: class, orderAmount: orderAmount, userName: userName)}}

CartViewModel class:

class CartViewModel {
var cartList = BehaviorSubject <[FoodsCart]>(worth: [FoodsCart]())
var frepo = FoodsDaoRepository()
var cart : FoodsCart



init() {
    getFromCart(userName: cart.userName)
    cartList = frepo.cartList
    print("CartViewModel")
}


func getFromCart(userName:String) {
    frepo.getFromCart(userName: userName)
}}

DetailScreen class:
I could not give a parameter to the picture in addToCart button as a result of the picture comes from API however Xcode desires it in UIImage knowledge kind

class DetailScreen: UIViewController {

@IBOutlet var detailImage: UIImageView!
var detailImageC : String!

@IBOutlet var foodNameLbl: UILabel!
var foodNameLblC : String!

@IBOutlet var totalPricelbl: UILabel!

@IBOutlet weak var minusBtn: UIImageView!
@IBOutlet weak var counterlbl: UILabel! // Counter label
@IBOutlet weak var plusBtn: UIImageView!

var foodCostLblC : String!



var foodId : Int!
var counter = 0
var title = ""

@IBOutlet var foodCategoryLbl: UILabel!
var foodCategoryLblC : String!


var meals: Meals?

var viewModel = DetailViewModel()

var detailToCart = CartScreen()


@IBOutlet var viewImage: UIView!
@IBOutlet weak var viewDetail: UIView!

override func viewDidLoad() {
    tremendous.viewDidLoad()
    
    foodNameLbl.textual content = foodNameLblC
    foodCategoryLbl.textual content = foodCategoryLblC
    costLbl.textual content = foodCostLblC
    configureImg()
    
    counterlbl.textual content = "0"
}


//Photos come from KingFisher
func configureImg() -> String { 
    let url = URL(string: "http://instance.com/meals/pictures/" + detailImageC!)
    detailImage?.kf.setImage(with: url)
}


//After I press Add to Cart button all the info ought to go to the Cart
@IBAction func addToCartBtn(_ sender: Any) {
    if let title = foodNameLbl.textual content, var picture = configureImg(), let worth = totalPricelbl.textual content, let class = foodCategoryLbl.textual content, let orderAmount = counterlbl.textual content {
        
        viewModel.insertToCart(title: title, picture : picture, worth: Int(worth)!, class: class, orderAmount: Int(orderAmount)!, userName: <#String#>)
        
    }
}}

Cart Display


    class CartScreen : UIViewController {

    
    @IBOutlet var cartTableView: UITableView!
    
    
    
    var foodsCart = [FoodsCart]()
    var viewModel = CartViewModel()

    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        
        cartTableView.delegate = self
        cartTableView.dataSource = self

        cartTableView.register(UINib(nibName: "ProductsTableViewCell", bundle: nil), forCellReuseIdentifier: "ProductsTableViewCell")
   }
    
    override func viewWillAppear(_ animated: Bool) {
        viewModel.getFromCart(userName: <#String#>)
        print("CartScreen")
    }    
}
extension CartScreen : UITableViewDelegate, UITableViewDataSource {
   
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return foodsCart.depend
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell = cartTableView.dequeueReusableCell(withIdentifier: "ProductCartCell", for: indexPath) as? ProductsTableViewCell else {return UITableViewCell()}  
        return cell   
    }}

ProductsTableViewCell class for Cart Display:

class ProductsTableViewCell: UITableViewCell {

@IBOutlet var productImageCart: UIImageView!

@IBOutlet var productNameCart: UILabel!

@IBOutlet var productAmountCart: UILabel!

@IBOutlet var userName: UILabel!

@IBOutlet var viewCart: UIView!

override func awakeFromNib() {
    tremendous.awakeFromNib()
    
}

override func setSelected(_ chosen: Bool, animated: Bool) {
    tremendous.setSelected(chosen, animated: animated)

    // Configure the view for the chosen state
} }

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles