Post

UIKit - 화면 전환 및 데이터 전달(코드로 구현)

UIKit - 화면 전환 및 데이터 전달(코드로 구현)

화면 전환 및 데이터 전달(코드로 구현)

UIViewController의 정의되어 있는 present, dismiss 메서드를 통해 구현 가능함.

Apple Develop Document

image

  • present: 화면 전환 역할을 담당
  • dismiss: 이전 화면으로 돌아가는 역할을 담당

구현 예시

ViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import UIKit

final class ViewController: UIViewController {
    
    private lazy var exBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("화면 이동", for: .normal)
        btn.backgroundColor = .black
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(exBtnTapped), for: .touchUpInside)
        
        return btn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    func setupUI() {
        view.addSubview(exBtn)
        
        exBtn.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            exBtn.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            exBtn.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            exBtn.widthAnchor.constraint(equalToConstant: 100),
            exBtn.heightAnchor.constraint(equalToConstant: 50)
        ])
    }
    
    @objc func exBtnTapped(){
        
        let convertVC = ConvertViewController()
        
        convertVC.exValue = "Hello World!" // 데이터 전달
        convertVC.modalPresentationStyle = .fullScreen // 화면 디자인 설정
        
        // 화면을 전환하는 역할을 수행함
        present(convertVC, animated: true)
    }
}

ConvertViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import UIKit

final class ConvertViewController: UIViewController {

    private lazy var rollBackBtn : UIButton = {
        let btn = UIButton()
    
        btn.setTitle("뒤로가기", for: .normal)
        btn.backgroundColor = .black
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(rollBackBtnTapped), for: .touchUpInside)
        
        return btn
    }()
    
    private lazy var exValueLabel: UILabel = {
        let label = UILabel()

        label.font = UIFont.boldSystemFont(ofSize: 22)
        
        return label
    }()
    
    // 데이터를 전달받기 위한 변수
    var exValue: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        exValueLabel.text = exValue // 넘어온 데이터 할당
    }
    
    func setupUI(){
        view.backgroundColor = .gray
        
        view.addSubview(rollBackBtn)
        view.addSubview(exValueLabel)
        
        rollBackBtn.translatesAutoresizingMaskIntoConstraints = false
        exValueLabel.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            rollBackBtn.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            rollBackBtn.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50),
            rollBackBtn.widthAnchor.constraint(equalToConstant: 100),
            rollBackBtn.heightAnchor.constraint(equalToConstant: 50),
            
            exValueLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            exValueLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    @objc func rollBackBtnTapped(){
        
        // 이전 화면으로 되돌아가게 하는 역할을 수행함
        dismiss(animated: true)
    }

}

위 예시 코드는 ViewController에서 버튼을 클릭하면 ConvertViewController 화면으로 전환되며, exValue 변수를 통해 데이터를 전달받음.

또한, modalPresentationStyle라는 UIViewController의 정의되어 있는 프로퍼티 값을 설정하여 화면이 전환될 때 어떤 형태(ex. 모달, 풀스크린 등)로 전환될지를 설정할 수 있음.

This post is licensed under CC BY 4.0 by the author.