iOS μμ νλ©΄ μ΄λμ νλ λ°©λ²μλ 4κ°μ§κ° μλ€.
- ViewController μμ νλ©΄ μ ν
- ViewController κ° λ€λ₯Έ ViewController λ₯Ό νΈμΆνμ¬ νλ©΄ μ ν
- NavigationViewController λ₯Ό μ¬μ©νμ¬ νλ©΄ μ ν
- νλ©΄ μ νμ© κ°μ²΄ Segue λ₯Ό μ¬μ©νμ¬ νλ©΄ μ ν
1. ViewController μμ νλ©΄ μ ν
ViewController μμ View λ₯Ό λ€λ₯Έ View λ‘ κ΅μ²΄νλ λ°©μμΌλ‘ λ©λͺ¨λ¦¬ λμλ‘ μΈν΄ μ¬μ©μ μ§μνλ€. νΉμ ν κ²½μ°μλ§ μ¬μ©νλ€. μλλ‘μ΄λμμλ View μ Visibility μμ±μ μ΄μ©νμ¬ λ³κ²½νλ λ°©μμ μμ£Ό μ¬μ©νμλλ° iOS μμλ μ§μνλ€κ³ νλ μ’ μ κΈ°νλ€. μ΄ λΆλΆμ λν΄μ μ’ λ μ°Ύμλ³΄κ³ μ 리ν΄λ΄μΌκ² λ€.
2. ViewController κ° λ€λ₯Έ ViewController λ₯Ό νΈμΆνμ¬ νλ©΄ μ ν
νλ¨μμ View κ° μ¬λΌμ€λ Modal λ°©μμ μ¬μ©νλ©΄ λλ€. μ νλ νλ©΄μ΄ μ΄λ€ λ°©μμΌλ‘ 보μ¬μ§ μ§μ λν μμ±μ μ μνλ©΄ μΌλ°μ μΈ νλ©΄ μ νμ²λΌ 보μ¬μ€ μλ μλ€.
ViewController λ₯Ό μΈμ€ν΄μ€ννλ κ³Όμ μμ μλ³μκ° μ¬μ©λκΈ° λλ¬Έμ μ€ν 리보λμμ ViewController μ λν μλ³μλ₯Ό μ μνλ€.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapMoveSecondViewController(_ sender: UIButton) {
guard let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController else { return }
// νλ©΄ μ ν μ λλ©μ΄μ
μ€μ
secondViewController.modalTransitionStyle = .coverVertical
// μ νλ νλ©΄μ΄ λ³΄μ¬μ§λ λ°©λ² μ€μ
secondViewController.modalPresentationStyle = .fullScreen
self.present(secondViewController, animated: true, completion: nil)
}
}
μ΄ λ°©λ²μΌλ‘ νλ©΄ μ νμ ꡬννλ©΄ μλ¨μ Back Button μ΄ κ΅¬νλμ΄ μμ§ μλ€. λ°λΌμ κ°λ°μκ° λ³λλ‘ Back Button μ ꡬνν΄μ€μΌ νλ€.
3. NavigationViewController λ₯Ό μ¬μ©
NavigationViewControllerλ₯Ό μ¬μ©νλ©΄ ViewControllerκ° Stackμ μμΈλ€. μλμ μΌλ‘ μλ¨ NavigationBarμ BackButtonμ΄ μΆκ°λκΈ° λλ¬Έμ 2λ² λ°©μκ³Ό λ€λ₯΄κ² λ³λλ‘ Back Buttonμ ꡬνν νμκ° μλ€.
Navigation Controllerλ₯Ό μΆκ°νλ©΄ 2κ°μ View κ° μΆκ°λλλ° Root View λ₯Ό μ κ±°νκ³ Navigation Controller μμ Ctrl + λλκ·Έ λ₯Ό ν΅ν΄ μ²μμ 보μ¬μ§ View μ μ°κ²°νλ€. μ¦, κΈ°λ³Έμ μΌλ‘ μμ±λ Root View λ₯Ό μ κ±°νκ³ μλ‘κ² Root View λ₯Ό μ€μ νλ κ²μ΄λ€.
Root View μ€μ μ μλ£νμΌλ©΄ νλ©΄ μ΄λμΌλ‘ 보μ¬μ€ λλ¨Έμ§ View λ₯Ό μΆκ°νλ€. μμ μλ³μλ₯Ό μΆκ°ν΄μ£Όλ κ²μ μμ§ λ§μ!
κ·Έ μ΄μ λ μΈμ€ν΄μ€ννλ κ³Όμ μμ μλ³μκ° μ¬μ©λκΈ° λλ¬Έμ΄λ€.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapMoveSecondViewController(_ sender: UIButton) {
guard let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController else { return }
self.navigationController?.pushViewController(secondViewController, animated: true)
}
}
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapMoveThirdViewController(_ sender: UIButton) {
guard let thirdViewController = self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") as? ThirdViewController else { return }
self.navigationController?.pushViewController(thirdViewController, animated: true)
}
}
μ€ν 리보λμμ μΆκ°ν μλ³μλ₯Ό κ°μ§κ³ ViewController λ₯Ό μΈμ€ν΄μ€ννκ³ pushViewController λ₯Ό μ¬μ©ν΄μ νλ©΄ μ νμ νλ€.
4. Segueλ₯Ό ν΅ν νλ©΄ μ ν
'π» κ°λ° > iOS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[TIL] 22.08.25 (0) | 2022.08.25 |
---|---|
[TIL] 22.08.24 (0) | 2022.08.24 |
[TIL] 22.05.27 (0) | 2022.05.27 |
[TIL] 22.05.26 (0) | 2022.05.26 |
[TIL] 22.04.20 (0) | 2022.04.20 |