Здравствуйте! Прохожу 2й курс, и при написании своего приложения возникла проблема с передачей данных между сценами. Проблема такая: в первом окне(1) я изменяю массив объектов(новостей) из второго окна, обращаясь к свойству(2), хранящемся в классе второго окна. В зависимости от того, какой длины массив, у меня должно изменяться количество строчек новостей. Покопавшись в коде, выяснил, что метод (1) выполняется все же раньше 3, но выполнение асинхронное, т.е. код не успевает сгенерить новости раньше, чем управление перейдёт в 3. Вопрос состоит в том, как задержать вызов (1) до вызова (3).
(1)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "NewsList" {
if let indexPath=tableView.indexPathForSelectedRow {
let nlc=segue.destination as! NewsListTableViewController
for index in 1...7
{
newsManager.fetchCurrentNewsWith(theme: "top-headlines?country=us&category=business", index: index) { (result) in
switch result {
case .Success(let currentNews):
print("here")
let news=currentNews
nlc.newsList.append(news)
case .Failure(let error as NSError):
let alertController = UIAlertController(title: "Unable to get data", message: "\(error.localizedDescription)", preferredStyle: .alert)
let okAction=UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true,completion: nil)
}
}
}
nlc.type="\(myCategories[indexPath.row].name)"
}
}
}
(2)
var newsList=[ News ] ()
(3)
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return newsList.count
}
(4)
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "News", for: indexPath) as! newsListTableViewCell
cell.authorLabel.text=self.newsList[indexPath.row].author
print(self.newsList[indexPath.row].author)
cell.titleLabel.text=self.newsList[indexPath.row].title
return cell
}