랭귀지/SWIFT
Swift 네비게이션 컨트롤러를 이용한 화면 전환
유키공
2018. 5. 30. 09:30
Swift 네비게이션 컨트롤러를 이용한 화면 전환
- 네비게이션 컨트롤러는 뷰 컨트롤러의 특별한 종류로 계층적인 성격을 띠는 콘텐츠 구조를 관리하기 위한 컨트롤러 입니다.(내비게이션 바가 내장되어 있습니다.)
- 이 컨트롤러가 제어하는 모든 뷰 컨트롤러에 내비게이션 바를 생성하는 특징이 있습니다.
- 루트뷰 컨트롤러는 내비게이션 컨트롤러에 직접 연결된 컨트롤러이므로 화면 UI상단에 내비게이션 바가 표시됩니다.
- 내비게이션 컨트롤러는 화면에 현재 표시되고 있는 뷰 컨트롤러들을 내비게이션 스택을 이용하여 관리합니다.
- 내비게이션 컨트롤러 최상위 뷰컨트롤러는(마지막컨트롤러), 최하위 컨트롤러는(루트뷰 컨트롤러)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func moveByNavi(_ sender: Any) {
//
guard let uvc = self.storyboard?.instantiateViewController(withIdentifier: "SecondVc") else {
return
}
self.navigationController?.pushViewController(uvc, animated: true)
}
@IBAction func movePresent(_ sender: Any) {
guard let uvc = self.storyboard?.instantiateViewController(withIdentifier: "SecondVc") else {
return
}
self.present(uvc, animated: true)
}
}
최상위 뷰 컨트롤러에서 제거할 때는 popViewController(animated:) -> 이전화면으로 돌아올때 사용
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func back(_ sender: Any) {
self.presentingViewController?.dismiss(animated: true)
}
@IBAction func back2(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}