스위프트로 화면전환을 구현하는 방식 중에 네비게이션 컨트롤러가 있다.
세그로 구현하는 것과 코드로 구현하는 것이 있는데 이 중에서도 푸쉬와 프레젠트라는 방식이 있다.
오늘은 이런 4가지 방식에 대한 포스팅을 해볼 예정!

▼ 실습 후 완성 화면
네비게이션 컨트롤러를 추가해주고 뷰컨트롤러에 세그 연결을 해준다.
네비게이션 기능을 구현해줄 각각의 버튼도 만들어준다.
총 4개의 실습을 할 것이라고 했는데
세그 연결로 네비게이션을 구현하는 뷰 컨트롤러 두 개와, 코드로 구현할 뷰 컨트롤러 두 개이다.
일단, Push와 Present 화면 전환 동작하는 모습이 다른데
Push는 방향이 가로(왼쪽->오른쪽), Presents는 세로(아래->위)이다.
또한 Push는 스택 형식이라 여러 뷰가 쌓일 수 있다.
① 세그로 푸쉬하는 방법
버튼과 컨트롤러를 세그로 연결해준다.
- Ctrl 누르고 커서를 끌어서 연결 or 오른쪽 마우스 끌어서 연결
Action Segue에 show로 연결해주면 된다.
사실 이러면 완성이다! 버튼을 클릭하면
import UIKit
class SeguePushViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func backBtn(_ sender: UIButton) {
//백 버튼 클릭 시 이전화면 이동
self.navigationController?.popViewController(animated: true)
//백 버튼 클릭 시 처음화면(루트 뷰) 이동
self.navigationController?.popToRootViewController(animated: true)
}
}
② 세그로 프레젠트
import UIKit
class SeguePresentViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func backBtn(_ sender: UIButton) {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
③ 코드로 푸쉬
④ 코드로 프레젠트
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func codePushBtn(_ sender: UIButton) {
guard let viewController = self.storyboard?.instantiateViewController(identifier:"codePushViewController") else { return }
self.navigationController?.pushViewController(viewController, animated: true)
}
@IBAction func codePresentBtn(_ sender: UIButton) {
guard let viewController = self.storyboard?.instantiateViewController(identifier: "codePresentViewController") else { return }
viewController.modalPresentationStyle = .fullScreen
self.present(viewController, animated: true, completion: nil)
}
}
다음글 : 데이터 전달
https://yejprogramming.tistory.com/65
[Swift/iOS프로그래밍] 뷰와 뷰 사이 데이터 전달
지난 화면전환 네비게이션 글에서 실습한 프로젝트 이후 부분이니 참고바람 https://yejprogramming.tistory.com/62 [swift/iOS프로그래밍] navigation controller 4가지 방법, 세그 vs 코드,push vs present 스위..
yejprogramming.tistory.com
'iOS > Swift실전' 카테고리의 다른 글
[Swift/iOS프로그래밍] 뷰와 뷰 사이 데이터 전달 (2) | 2022.05.25 |
---|---|
[Swift/iOS프로그래밍] 뷰 사이클과 관련된 메서드들 (0) | 2022.05.25 |
[iOS] textfield와 label (0) | 2022.01.25 |
[iOS]this class is not key value coding-compliant for the key (2) | 2022.01.22 |
[iOS] 할 일 체크리스트 앱 만들기! To do list (0) | 2022.01.19 |