Swiftにて、modalをdismissしたタイミングで親ViewのtableviewをreloadDataしたいと思います。
前提条件
親viewでは展開時に処理待ちの通知の一覧をAPIから取得してきてtableviewに表示。
tebleviewcell内のボタンで処理内容を選択し、処理内容に応じmodalを展開。
親view内の通知一覧の画面表示内容を更新しないとならないので
modalの戻るボタンを押したタイミングで、親view展開時の描画のfunctionを実行している。
発生している問題・エラーメッセージ
modalを閉じた際に self?.tableView.reloadData() が動作しない。
self?.tableViewがnilになってしまっている。
該当のソースコード
親view
swift
1 2class NotificationViewController: UIViewController { 3 4〜中略〜 5 6 func refresh(){ 7 if let loginData = self.loginData, 8 let accountId = loginData.accountId { 9 self.accountId = accountId 10 } 11 12 self.alerts = [] 13 self.sendAlerts() 14 } 15} 16 17extension NotificationViewController { 18 func sendAlerts() { 19 20〜中略〜 21 22 let parameters = ["account_id": self.accountId] 23 API.Messages.alerts(parameters: parameters) 24 .response({ [weak self] result in 25 switch result { 26 case let .response(message): 27 self?.alerts = message.alerts 28 DispatchQueue.main.async { 29 self?.tableView.reloadData() 30 if message.alerts.count == 0 { 31 AlertUtility.showConfirm(viewController: self, message: NSLocalizedString("noNotificationFailure", comment: "noNotificationFailure")) 32 } 33 } 34 case let .error(reason): 35 DispatchQueue.main.async { 36 AlertUtility.showError(viewController: self, message: NSLocalizedString("alertsFailure", comment: "alertsFailure")) 37 } 38 } 39 }) 40 self.reloadButton.isEnabled = true 41 } 42 43
モーダル
swift
1 2class PopupViewController: UIViewController { 3 4〜中略〜 5 6 @IBAction func okDate_Action(_ sender: Any) { 7 dismiss(animated: true) 8 guard let parent = storyboard?.instantiateViewController(withIdentifier: "NotificationViewController") as? NotificationViewController else { 9 fatalError() 10 } 11 parent.refresh() 12 } 13} 14
回答2件
あなたの回答
tips
プレビュー