iOS/UIKIT

화면 이동과 데이터 전달 1편

밤새는 탐험가89 2024. 1. 14. 17:52

 

 

ViewController에서 DetailViewController로 화면 전환 구현

(UI는 코드로 구현, Main 파일은 삭제하지 않고 진행)

 

 

최종 구현

 

 

 

화면 전환에 사용되는 함수 

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621380-present

 

present(_:animated:completion:) | Apple Developer Documentation

Presents a view controller modally.

developer.apple.com

 

 

 

화면 전환되기 전으로 되돌아가기 위해 사용되는 함수 

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss

 

dismiss(animated:completion:) | Apple Developer Documentation

Dismisses the view controller that was presented modally by the view controller.

developer.apple.com

 

 

 

실제로 구현 코드 

 

 

 

 

ViewController 파일

import UIKit

class ViewController: UIViewController {
    
    // 라벨 구현
    private var mainLabel: UILabel = {
        var label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = "메인 화면"
        label.font = .systemFont(ofSize: 22, weight: .bold)
        label.textColor = .green
        return label
    }()
    
    // 버튼 구현
    private var moveToDetailButton: UIButton = {
        var button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("NEXT", for: .normal)
        button.setTitleColor(UIColor.label, for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 22, weight: .bold)
        button.backgroundColor = UIColor.green
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 10
        
        // 다음 페이지로 이동 함수
        button.addTarget(Any?.self, action: #selector(moveToDetail), for: .touchUpInside)
        
        return button
    }()
    
    // 버튼 함수 구현
    @objc func moveToDetail() {
        let detailVC = DetailViewController()
        
        detailVC.mainMessage = "성공"
        
        detailVC.modalPresentationStyle = .fullScreen
        present(detailVC, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .red
        
        view.addSubview(mainLabel)
        view.addSubview(moveToDetailButton)
        
        configureConstraints()
    }
    
    // 제약 조건
    func configureConstraints() {
        
        let mainLabelContraints = [
            mainLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            mainLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ]
        
        let moveToDetailButtonConstraints = [
            moveToDetailButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            moveToDetailButton.topAnchor.constraint(equalTo: mainLabel.bottomAnchor, constant: 20),
            moveToDetailButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30),
            moveToDetailButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
        ]
        
        
        NSLayoutConstraint.activate(mainLabelContraints)
        NSLayoutConstraint.activate(moveToDetailButtonConstraints)
    }
}

 

 

 

 

DetailViewController 파일

import UIKit

class DetailViewController: UIViewController {
    
    // MARK: 메인 페이지에서 데이터 받을 변수
    // MARK: 데이터를 주고 받을 때는 변수를 통할 것
    var mainMessage: String?

    
    // 디테일 라벨 설정
    private var detailLabel: UILabel = {
        var label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = ""
        label.font = .systemFont(ofSize: 22, weight: .bold)
        label.textColor = .green
        return label
    }()
    
    // 버튼 구현
    private var BackToMainButton: UIButton = {
        var button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("Back", for: .normal)
        button.setTitleColor(UIColor.label, for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 22, weight: .bold)
        button.backgroundColor = UIColor.green
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 10
        
        // 버튼 함수 구현
        button.addTarget(Any?.self, action: #selector(backToMain), for: .touchUpInside)
        return button
    }()
    
    // 버튼 함수
    @objc func backToMain() {
        dismiss(animated: true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .blue

        view.addSubview(detailLabel)
        view.addSubview(BackToMainButton)
        
        detailLabel.text = mainMessage
        configureConstraints()
    }
    
    // 제약 조건
    func configureConstraints() {
        
        let detailLabelContraints = [
            detailLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            detailLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ]
        
        let moveToDetailButtonConstraints = [
            BackToMainButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            BackToMainButton.topAnchor.constraint(equalTo: detailLabel.bottomAnchor, constant: 20),
            BackToMainButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30),
            BackToMainButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
        ]
        
        
        NSLayoutConstraint.activate(detailLabelContraints)
        NSLayoutConstraint.activate(moveToDetailButtonConstraints)
    }
}