본문 바로가기

iOS/Swift

[iOS UIKint in Swift 4] UINavigationController 사용하기 (Display UINavigationController)

UINavigationController 사용하기

UINavigationController를 사용하여 ViewController를 이동해 보았습니다.

pushViewController를 하게되면 ViewController stack에 추가되고

popViewController를 하게되면 ViewController stack에서 사라지게 됩니다.


navigationController에서 제공하는 함수를 사용하여 ViewController를 이동할 수 있습니다.

1. func pushViewController(UIViewController, animated: Bool)

해당 ViewController로 이동하고 stack에 추가합니다.

2. func popViewController(animated: Bool) -> UIViewController?

stack에 쌓여있는 이전 ViewController로 이동하게 됩니다. 현재 ViewController는 사라집니다.

3. func popToRootViewController(animated: Bool) -> [UIViewController]?

navigationController에서 지정해놓은 Root ViewController로 이동합니다.
stack에 쌓여있는 ViewController는 모두 사라지게 됩니다.

4. func popToViewController(UIViewController, animated: Bool) -> [UIViewController]?

지정된 ViewController로 이동하고 전에 있던 ViewController들은 stack에서 사라집니다.


아래 이미지와 소스코드를 비교해보시면 좀 더 이해하기 편합니다.

궁금하신점은 댓글로 달아주세요.


해피코딩 :)


Preview


Source

//
// FirstViewController.swift
// UIKit component handling
//
// Created by Taehyeon Han on 2018. 8. 3..
// Copyright © 2018년 calmone. All rights reserved.
//
import UIKit
class FirstViewController: UIViewController {
lazy var button: UIButton = {
// Generate UIButton.
let width: CGFloat = 300
let height: CGFloat = 100
let posX: CGFloat = (self.view.bounds.width - width)/2
let posY: CGFloat = 200
let button = UIButton(frame: CGRect(x: posX, y: posY, width: width, height: height))
button.backgroundColor = UIColor.orange
button.layer.masksToBounds = true
button.layer.cornerRadius = 20.0
button.setTitle("Move to SecondView.", for: .normal)
button.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Set the controller's title.
self.navigationItem.prompt = "Fisrt View"
// Set the background color of the view to Cyan.
self.view.backgroundColor = .orange
// Add UIButton on view
self.view.addSubview(self.button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// UIButton Event.
@objc private func buttonPressed(_ sender: Any) {
// Define the view to move to.
let secondViewController = SecondViewController()
// Move to SecondView.
self.navigationController?.pushViewController(secondViewController, animated: true)
}
}
//
// SecondViewController.swift
// UIKit component handling
//
// Created by Taehyeon Han on 2018. 8. 3..
// Copyright © 2018년 calmone. All rights reserved.
//
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Set the controller's title.
self.navigationItem.prompt = "Second View"
// Set the background color of the view to Cyan.
self.view.backgroundColor = .green
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}


Github

https://github.com/calmone/iOS-UIKit-component


Reference