我有两个VC。第一个是MealsVC,餐桌上摆满了餐点。第二个是DetailsVC(膳食详情)。当用户从桌面视图中选择一餐时DetailsVC
弹出,当用户点击X按钮时DetailsVC
被取消。然而,饭菜再次重新加载,它会产生双餐(6餐而不是3餐)
我在某个地方找到了解决方案,但它根本不起作用。以下是我从DetailsVC
解雇MealsVC
(来自解决方案)
@IBAction func back(_ sender: Any) {
DataManager.shared.firstVC.tableView.reloadData()
dismiss(animated: true, completion: nil)
}
和' MealsVC`
override func viewDidLoad() {
super.viewDidLoad()
DataManager.shared.firstVC = self
并创建了singleton
import Foundation
class DataManager {
static let shared = DataManager()
var firstVC = MealsVC()
}
我还尝试从detailsVC
到MealsVC
执行Segue,但tableView没有重新加载,它显示一个空的tableview
更新
在MealsVC中获取数据:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
getMeals()
}
func getMeals () {
Alamofire.request(url!, method: .post, parameters: para,encoding: URLEncoding.default, headers: headers).responseJSON { response in
if let value : AnyObject = response.result.value as AnyObject {
let meals = JSON(value)
// get meals code
let info = Meals(meal_id: id, Logo: logoString, meal_name: name, price: price)
self.meals.append(info)
}
}
DispatchQueue.main.async{
self.tableView.reloadData()
}
} else {
print("sorry data from api has some errors")
}
}
}