준코딩

(ios/Swift) 화면전환 - present 본문

프로그래밍/IOS (Swift)

(ios/Swift) 화면전환 - present

Ljunhyeob - App Dev 2022. 12. 21. 11:35

Xcode : 14.2v

사용언어: Swift , StoryBoard

깃허브주소: https://github.com/Ljunhyeob/ChangeControllerPresent

 

 

이번에는 Present 를 이용하여 화면전환을 하는 예제를 만들어 보겠습니다.

(Segue 로 화면전환 하는 예제: https://leejhjava.tistory.com/entry/iosSwift-%ED%99%94%EB%A9%B4%EC%A0%84%ED%99%98-Segue)

 

1.우선 기존 컨트롤러 옆에 새로운 컨트롤러를 생성해 줍니다.

 

2. 각각 첫번째 화면과 새로만든 화면에 첫번째 화면, 두번째 화면 이라는 label을 생성하고, 각각 다음 화면 버튼, 이전 화면 버튼 을 생성해 줍니다. 

 

 

3. 그리고 좌측 하단 + 버튼, File을 차례로 누르시고 

 

4. ios / Swift File 을 생성해 줍니다.

 

5. 두번째 화면에 대한 컨트롤러를 만드는 것이니 저는 SecondController로 이름 짓겠습니다.

 

6. 방금 생성한 SecondController 에 아래와 같이 코드를 작성해 주세요.

import UIKit


class SecondController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
}

 

7. 그리고 다시 스토리보드로 돌아와 첫번째 화면을 선택 후(상단 3가지 버튼 중 제일 좌측에 있는걸 누르면 선택 됩니다.) 우측 인스펙터창 에서 4번째 선택하시고 Class 이름과 하단에 Storyboard ID를 작성해주세요. 

 

 

8. 그리고 이미 생성해 두었던 버튼을 컨트롤러와 연결 시켜줍니다. (첫번째 화면 버튼은 기존에 생성되어있던 ViewController에 연결)

 

9. 이번에는 두번째 화면을 선택해 주시고 동일하게 class 이름과 sotryboard Id 를 작성해주세요. (아까 파일 생성할때 SecondController로 만들었기 때문에 저는 둘다 SecondController로 작성했습니다.)

 

10.  위 8번과 같이 SecondController에 이전화면 버튼을 연결시켜 줍니다. 

 

11. 그리고 다시 viewController에 다음화면 버튼 안쪽에 코드를 작성해 줍니다.

 

 

 import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }




    @IBAction func nextBtn(_ sender: Any) {
        guard let nextVC = self.storyboard?.instantiateViewController(identifier: "SecondController") else {return}
        self.present(nextVC ,animated: true)
    }
}

 

12.다시 SecondController에 오셔서 이전화면 버튼 안에 코드를 작성해 줍니다.

 

import UIKit


class SecondController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


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

 

그러면 정상적으로 화면이 이동하는걸 보실 수 있을겁니다.

그런데 화면 뜨는 창이 마음에 안들더라구요 그래서 아래 코드에 각각 화면 뜨는 설정을 변경할 수 있는걸 주석처리로 설명해 두었으니 맞게 사용하시면 됩니다.

아래 소스는 viewController 에서 수정하시면 됩니다.

//
//  ViewController.swift
//  ChangePersent
//
//  Created by 이준협 on 2022/12/21.
//


import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }




    @IBAction func nextBtn(_ sender: Any) {
        guard let nextVC = self.storyboard?.instantiateViewController(identifier: "SecondController") else {return}
        //nextVC.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        //꽉찬 화면으로 이동
        
        //nextVC.modalPresentationStyle = UIModalPresentationStyle.currentContext
        //fullScreen과 다를게 없어보이지만, 차이점은 무조건 디바이스 화면에 꽉차에 표시되는 fullScreen과 달리 현재 viewController의 크기에 맞춰서 불러진다. (지금 view가 작다면 currentContext 로 설정해서 부른 화면도 작게 불린다는 의미.)
        
        //nextVC.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
        //nextVC.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
        //위에서 설명한 것과 같지만 하나 다른점은 alpha 값을 줄 수 있다는 점입니다. alpah 값은 투명도를 변경할 수 있습니다. (기존 화면도 뒤에 보이기 원할때 사용합니다.)
        
        self.present(nextVC ,animated: true)
    }
}

Comments