[iOS] 화면전환 Push와 Present

김민석·2024년 8월 22일
0

iOS

목록 보기
9/12
post-thumbnail

차이점

PresentPush
뷰 전개세로(아래에서 위)가로(왼쪽에서 오른쪽)
방식화면을 다른 화면 위로 띄워 표현하는 방식새로운 화면을 viewControllers에 새롭게 추가시키고 topViewController로 설정
어느 class로 부터의 메소드UIViewController의 methodUINavigationController의 method
예제alert 등 view 위에 다른 무언가 띄울때도 사용NavigationController를 통해 뭔가 이동하고 뒤로가고 할때 많이 사용

Present

Push




Present: 뷰 이동


UIViewController | Apple Developer Documentation

UIViewController: UIKit 앱의 뷰 계층을 관리하는 객체입니다.

  • UIViewController 클래스의 method
  • 전환 방식: 세로 방향
  • modalPresentationStyle로 화면 전환이 되는 스타일을 정할 수 있음!

Presents a view controller modally.

func present(
    _ viewControllerToPresent: UIViewController,
    animated flag: Bool,
    completion: (() -> Void)? = nil
)
  • viewControllerToPresent - 전환될 뷰
  • animated - 애니메이션 여부
  • completion - 이동하고 나서 실행될 문장 블록

💡 사용법

@IBAction func present(_ sender: Any) {
    let twoVC =  self.storyboard?.instantiateViewController(withIdentifier: "twoViewController")

    twoVC?.modalPresentationStyle = .fullScreen
    self.present(twoVC!, animated: true, completion: nil)
}

dismiss: 돌아가기


present된 뷰를 메모리에서 삭제하고 걷어내기

func dismiss(
    animated flag: Bool,
    completion: (() -> Void)? = nil
)

💡 사용법

@IBAction func dismiss(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}

Present 결과 계층구조





Push: 뷰 이동


pushViewController(_:animated:) | Apple Developer Documentation

pushViewController: 계층적 콘텐츠를 탐색하기 위한 스택 기반 체계를 정의하는 컨테이너 뷰 컨트롤러

  • pushViewControllermethod
  • 전환 방식: 가로 방향
  • 스택에 뷰를 쌓는 형태로 화면을 업데이트
func pushViewController(
    _ viewController: UIViewController,
    animated: Bool
)

💡 사용법

@IBAction func push(_ sender: Any) {
    let twoVC =  self.storyboard?.instantiateViewController(withIdentifier: "twoViewController")

    self.navigationController?.pushViewController(twoVC!, animated: true)
}

pop: 돌아가기


스택에 쌓인 뷰 1개를 pop하고 돌아가기

func popViewController(animated: Bool) -> UIViewController?

💡 사용법

@IBAction func pop(_ sender: Any) {
    self.navigationController?.popViewController(animated: true)
}

popToRootViewController: Root로 돌아가기


func popToViewController(
    _ viewController: UIViewController,
    animated: Bool
) -> [UIViewController]?
  • 반환 값: 스택에서 pop된 항목을 나타내는 뷰 컨트롤러 배열

💡 사용법

@IBAction func gotoNextVC(_ sender: Any) 
  guard let signUpVC =  self.storyboard?.instantiateViewController(withIdentifier: "SignUpNickNameVC")  else {return} 
	welcomeVC.modalPresentationStyle = .fullScreen

	self.present(welcomeVC, animated: true, completion: {
		self.navigationController?.popToRootViewController(animated: true)
	})
}

popTo: 원하는 스택으로 돌아가기


func popToViewController(
    _ viewController: UIViewController,
    animated: Bool
) -> [UIViewController]?
  • viewController: 스택의 맨 뒤에 두려는 뷰 컨트롤러(해당 VC는 현재 탐색 스택에 있어야함)
  • 반환값: 스택에서 pop된 항목을 나타내는 뷰 컨트롤러 배열

💡 사용법

@IBAction func gotoBack(_ sender: Any) 
  guard let signUpVC =  self.storyboard?.instantiateViewController(withIdentifier: "SignUpNickNameVC")  else {return} 
	welcomeVC.modalPresentationStyle = .fullScreen

	self.present(welcomeVC, animated: true, completion: {
		//navigation 스택에서 돌아가고싶은 뷰까지 pop하면서 이동 
		if let rootVC = ViewControoller as? LoginVC {
			self.navigationController?.popToViewController(rootVC ,animated: true)
		}
	})
}

Push 결과 View 계층 구조




push와 Present의 화면 이동 확인

1. push와 present 계층 구조 보기

어떤 View가 내려가고 올라가는지 확인 방법⁉️

VC의 생명주기를 이용해서 어떤 부분이 불러지고 꺼지는지 아래 method를 통해 확인!

extension ViewController {
    override func viewWillAppear(_ animated: Bool) {
        print("mainViewController", #function)
        super.viewWillAppear(animated)
    }
    override func viewIsAppearing(_ animated: Bool) {
        print("mainViewController", #function)
        super.viewIsAppearing(animated)
    }
    override func viewDidAppear(_ animated: Bool) {
        print("mainViewController", #function)
        super.viewDidAppear(animated)
    }
    override func viewWillDisappear(_ animated: Bool) {
        print("mainViewController", #function)
        super.viewWillDisappear(animated)
    }
    override func viewDidDisappear(_ animated: Bool) {
        print("mainViewController", #function)
        super.viewDidDisappear(animated)
    }
}

참고 자료

present(_:animated:completion:) | Apple Developer Documentation
pushViewController(_:animated:) | Apple Developer Documentation
iOS | present vs push
[iOS] 화면 전환 방식 push vs present
https://medium.com/@zzzzot/a-comprehensive-comparison-when-to-push-to-uinavigationcontroller-vs-when-to-present-modally-d9f54be2e11e

profile
개발을 배우는 대학생입니다!

0개의 댓글

관련 채용 정보