画面の呼び出し
呼び出される画面
class NotificationDetaliViewController: UIViewController { var notification: UserNotification! var result: Bool // storyboardでidentirierを設定済み static func fromStoryboard() -> NotificationDetaliViewController? { return UIStoryboard(name: "Main", bundle: nil) .instantiateViewController(withIdentifier: "NotificationDetaliViewController") as? NotificationDetaliViewController } override func viewDidLoad() { super.viewDidLoad() } }
呼び出す側(ナビゲーションプッシュの場合)
guard let nextView = NotificationDetaliViewController.fromStoryboard() else { return } //データを渡す nextView.notification = notificationList[indexPath.row] navigationController?.pushViewController(nextView, animated: true)
呼び出す側(モーダルの場合)
guard let nextView = NotificationDetaliViewController.fromStoryboard() else { return } nextView.modalPresentationStyle = .fullScreen present(nextView, animated: false, completion: nil)
画面を閉じる
前の画面に値を渡す
// ナビゲーションコントローラの取得 guard let navigation = self.navigationController else { print("Could not find navigationController") return } // 前の画面のインスタンスを取得 guard let prevView = navigation.viewControllers[navigation.viewControllers.count - 2] as? NotificationViewController else { print("Could not find previous viewController") return } // 前の画面に値を渡す prevView.result = true
前の画面に値を渡す(モーダルの場合)
// ナビゲーションコントローラの取得 guard let navigation = presentingViewController as? UINavigationController else { print("Could not find navigationController") return } // 前の画面のインスタンスを取得 guard let prevView = navigation.viewControllers[navigation.viewControllers.count - 1] as? SearchFriendViewController else { print("Could not find previous viewController") return } // 前の画面に値を渡す prevView.result = true
今の画面を閉じる
navigationController?.popViewController(animated: true)
今の画面を閉じる(モーダル)
dismiss(animated: true)