Skip to content

Instantly share code, notes, and snippets.

@showcove
Last active September 3, 2021 08:29
Show Gist options
  • Save showcove/c43a5a08e3fe22133753485ebd21e2d8 to your computer and use it in GitHub Desktop.
Save showcove/c43a5a08e3fe22133753485ebd21e2d8 to your computer and use it in GitHub Desktop.
Navigation vs Modal Full Source-Code
//
// ViewController.swift
// PushVsModal
//
// Created by jay on 2021/08/28.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
makeTitle()
}
@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 popToGrandParentViewController() {
guard let navigationVC = self.navigationController,
navigationVC.viewControllers.count > 2 else {
let alert = UIAlertController(title: nil,
message: "더 이상 Pop to Grand-Parent 동작을 실행할 수 없습니다.",
preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "확인",
style: .default,
handler: nil)
alert.addAction(confirmAction)
self.present(alert, animated: true, completion: nil)
return
}
let currentStackCount = navigationVC.viewControllers.count
let targetViewController = navigationVC.viewControllers[currentStackCount - 3]
self.navigationController?.popToViewController(targetViewController, animated: true)
}
}
// ViewController 의 Title 작성하는 함수
extension UIViewController {
func makeTitle() {
self.title = "Navigation-Stack : \(navigationController?.viewControllers.count ?? 1)"
var stackContents: [String] = []
if let navigationStack = navigationController?.viewControllers {
for (index, vc) in navigationStack.enumerated() {
stackContents.append("\(index) - \(vc)")
}
}
print("Stack :\n\(stackContents.joined(separator: "\n"))\n")
}
}
// 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)
if #available(iOS 13.0, *) {
return storyboard.instantiateViewController(identifier: identifiler)
} else {
return storyboard.instantiateViewController(withIdentifier: identifiler)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment