处理来自多viewController的项目

问题描述

您好,我正在尝试构建订购食品应用程序,但我遇到了一个问题,我想为多个viewController分隔cartTableViewController。 我不知道如何解释它,因为我的英语太糟糕了,但是我会尽力解释它。。。我的意思是说,用户已经从burgerKingViewController添加到cartTableViewController了,然后他继续使用mcdonaldsViewController将项目添加到cartTableViewController,在这里,我想向用户显示一个警报,说他在burgerKingViewController的购物车中有一个项目,应该删除它,以便可以从mcdonaldsViewController添加项目

为了进一步简化它..我希望cartTableViewController仅处理一个餐厅,这样我才能从firebase接受订单。

谢谢。

这是我用来将商品添加到cartTableViewController的代码

import UIKit

struct userData {
static var selectedItem: Item? = nil
static var selectedItems: [Item] = []



static func selectedItemsPrice() -> Double {
    var result: Double = 0
    for item in selectedItems {
        result = result + item.price
    }
    return result
 }

 static func allItemsToString() -> String {
    var allNames: [String] = []
    for item in selectedItems {
        allNames.append(item.name)
    }
    return allNames.joined(separator: ",")
  }



  }


struct Item: Equatable {

static let items : [Item] = {
    let food: Item = .init(image: UIImage(named: “food”)!,name: “food,price: 50)
    return [food]
  }()
var image: UIImage?
var name: String
var price: Double

  }

这是cartTableViewController

 import UIKit


 class cartTableViewController: UITableViewController {

 @IBOutlet var checkOutpressed: UIButton!
 @IBOutlet var priceLabel: UILabel!
 @IBOutlet var cartView: UIView!
 @IBOutlet var totalOrder: UILabel!
 @IBOutlet var cartTableView: UITableView!


 override func viewDidLoad() {
    
    super.viewDidLoad()
    
    overrideUserInterfaceStyle = .light
    
    checkOutpressed.layer.cornerRadius = 4.0
    
  }



  override func viewWillAppear(_ animated: Bool) {
    
    priceLabel.text = "\(userData.selectedItemsPrice())"
    totalOrder.text = "\(userData.allItemsToString())"
    
    
    
    
    super.viewWillAppear(animated)

  }


  override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation,return the number of sections
    return 1
  }

  override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int)   -> Int {
    // #warning Incomplete implementation,return the number of rows
    
    
    
    
    
    return userData.selectedItems.count
    
    
    
}



override func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
    105
}







override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell") as! cartTableViewCell
    
    
    
    
    
    
    
    cell.cartimage.layer.masksToBounds = true
    cell.cartimage.layer.cornerRadius = 8
    cell.cartimage.translatesAutoresizingMaskIntoConstraints = false
    cell.cartimage.contentMode = .scaleAspectFill
    cell.cartLabel.textColor = .black
    cell.cartLabel.translatesAutoresizingMaskIntoConstraints = false
    cell.selectionStyle = .none
    
    
    
    
    let cart = userData.selectedItems[indexPath.row]
    
   
    cell.cartimage.image = cart.image
    cell.cartLabel.text = cart.name
    cell.priceLabel1.text = “\(cart.price)"
    

    return cell


}







// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView,canEditRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}



// Override to support editing the table view.




// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView,moveRowAt fromIndexPath: IndexPath,to: IndexPath) {
    
}



// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView,canMoveRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the item to be re-orderable.
    return true
}

override func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
    
    
    
    
    
    
    
}

// MARK: - Navigation

// In a storyboard-based application,you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}


override func tableView(_ tableView: UITableView,commit editingStyle: UITableViewCell.EditingStyle,forRowAt indexPath: IndexPath) {
    
    if editingStyle == .delete {
        
        // remove the item from the data model
        userData.selectedItems.remove(at: indexPath.row)
        // delete the table view row
        tableView.deleteRows(at: [indexPath],with: .fade)
        priceLabel.text = "\(userData.selectedItemsPrice())"
        totalOrder.text = "\(userData.allItemsToString())"
        
        
        
        
    } else if editingStyle == .insert {
        // Not used in our example,but if you were adding a new row,this is where you would do it.
        
    }
}



@IBAction func checkOutpressed(_ sender: UIButton) {
    
    
    
    if priceLabel.text! == "" || totalOrder.text! == "" {
        
        // Alert
        let optionMenu = UIAlertController(title: nil,message: “please add item”,preferredStyle: .alert)
        
        // Add actions to the menu
        let cancelAction = UIAlertAction(title: "OK",style: .cancel,handler:
                                            nil)
        optionMenu.addAction(cancelAction)
        
        // display the menu
        self.present(optionMenu,animated: true,completion: nil)
        
        
        
        
        
        
    }
    
    
    
}


}

这是burgerKingViewController

 import UIKit

 class burgerKingViewController: UIViewController {


@IBOutlet var burgerKingImage: UIImageView!
@IBOutlet var burgerKingName: UILabel!
@IBOutlet var burgerKingPrice: UILabel!
@IBOutlet var makeOrder: UIButton!

override func viewDidLoad() {
    
super.viewDidLoad()
    
    overrideUserInterfaceStyle = .light
    makeOrder.layer.cornerRadius = 4.0



  }




@IBAction func makeOrder(_ sender: UIButton) {
    
    
    
    let alert = UIAlertController(title: "",message: “done”,preferredStyle: .alert)
    self.present(alert,completion: nil)
    
    let when = dispatchTime.Now() + 1
    dispatchQueue.main.asyncAfter(deadline: when) {
        // your code with delay
        alert.dismiss(animated: true,completion: nil)
    }
    
    
    userData.selectedItems.append(Item(image: UIImage(named: “burgerKing”),name: “burgerKing”,price: Double(10.000)))
    
    
    
    
    self.aimateView(sender)
    
    
    
    
}




fileprivate func aimateView( _ viewToAnimate:UIView) {
    
    UIView.animate(withDuration: 0.5,delay: 0,usingSpringWithdamping: 0.2,initialSpringVeLocity: 2,options: .curveEaseIn,animations: {
        
        viewToAnimate.transform = CGAffineTransform(scaleX: 0.92,y: 0.92)
        
        
        
    }) { (_) in
        
        UIView.animate(withDuration: 0.5,usingSpringWithdamping: 0.4,animations: {
            
            viewToAnimate.transform = CGAffineTransform(scaleX: 1,y: 1)
            
        },completion: nil)
    }
   }






}

这是mcdonaldsViewController

import UIKit

class mcdonaldsViewController: UIViewController {


@IBOutlet var mcdonaldsImage: UIImageView!
@IBOutlet var mcdonaldsName: UILabel!
@IBOutlet var mcdonaldsPrice: UILabel!
@IBOutlet var makeOrder: UIButton!

override func viewDidLoad() {
    
    
    super.viewDidLoad()
    overrideUserInterfaceStyle = .light
    
    makeOrder.layer.cornerRadius = 4.0

 }





@IBAction func makeOrder(_ sender: UIButton) {
    
    
    
    let alert = UIAlertController(title: "",completion: nil)
    
    // change to desired number of seconds (in this case 5 seconds)
    let when = dispatchTime.Now() + 1
    dispatchQueue.main.asyncAfter(deadline: when) {
        // your code with delay
        alert.dismiss(animated: true,completion: nil)
    }
    
    
    
    userData.selectedItems.append(Item(image: UIImage(named: "mcdonalds"),name: "mcdonalds",completion: nil)
    }
}






}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)