Created
November 7, 2021 00:43
-
-
Save showcove/4f00ddda65bdb5639943cf74ef5766de to your computer and use it in GitHub Desktop.
PushVsModal_2 : Final
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// PushVsModal | |
// | |
// Created by jay on 2021/11/06. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
makeTitle() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
printVCInfo() | |
} | |
@IBAction func pushViewController() { | |
let targetViewController = UIViewController.viewControllerFromMainStoryboard(identifier: ViewController.storyboardIdentifier) | |
self.navigationController?.pushViewController(targetViewController, animated: true) | |
} | |
@IBAction func popViewController() { | |
guard let stackCount = self.navigationController?.viewControllers.count, | |
stackCount > 1 else { | |
let alert = UIAlertController(title: nil, | |
message: "더 이상 Pop 동작을\n실행할 수 없습니다.", | |
preferredStyle: .alert) | |
let confirmAction = UIAlertAction(title: "확인", | |
style: .default, | |
handler: nil) | |
alert.addAction(confirmAction) | |
self.present(alert, animated: true, completion: nil) | |
return | |
} | |
self.navigationController?.popViewController(animated: true) | |
} | |
@IBAction func popToRootViewController() { | |
guard let stackCount = self.navigationController?.viewControllers.count, | |
stackCount > 1 else { | |
let alert = UIAlertController(title: nil, | |
message: "더 이상 Pop 동작을\n실행할 수 없습니다.", | |
preferredStyle: .alert) | |
let confirmAction = UIAlertAction(title: "확인", | |
style: .default, | |
handler: nil) | |
alert.addAction(confirmAction) | |
self.present(alert, animated: true, completion: nil) | |
return | |
} | |
self.navigationController?.popToRootViewController(animated: true) | |
} | |
@IBAction func presentViewController() { | |
let targetViewController = UIViewController.viewControllerFromMainStoryboard(identifier: ViewController.storyboardIdentifier) | |
let navigationController = UINavigationController(rootViewController: targetViewController) | |
navigationController.modalPresentationStyle = .fullScreen | |
self.present(navigationController, animated: true) | |
} | |
@IBAction func dismissViewController() { | |
self.dismiss(animated: true) | |
} | |
} | |
// ViewController 의 Title 작성하는 함수 | |
extension UIViewController { | |
func makeTitle() { | |
var modalDepth = 1 | |
var currentPresentingViewController: UIViewController? = self.presentingViewController | |
while (currentPresentingViewController != nil) { | |
modalDepth += 1 | |
currentPresentingViewController = currentPresentingViewController?.presentingViewController | |
} | |
self.title = "Modal:\(modalDepth)-Navi:\(navigationController?.viewControllers.count ?? 1)" | |
} | |
func printVCInfo() { | |
print("------") | |
print("[Self] :", title ?? "") | |
print("[Self's Navigation VC] :", self.navigationController ?? "") | |
print("[Self's Presenting VC] :", self.presentingViewController) | |
print("[Parent VC's Presented VC] :", self.presentingViewController?.presentedViewController) | |
} | |
} | |
// Storyboard 에서 ViewController 를 생성하기 위한 Util 함수들 | |
extension UIViewController { | |
static var storyboardIdentifier: String { | |
return String(describing: self) | |
} | |
static func viewControllerFromMainStoryboard(identifier: String) -> UIViewController { | |
return viewController(storyboardName: "Main", identifiler: identifier) | |
} | |
static func viewController(storyboardName: String, identifiler: String) -> UIViewController { | |
let storyboard = UIStoryboard(name: storyboardName, bundle: nil) | |
return storyboard.instantiateViewController(identifier: identifiler) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment