[Swift] TableView에서 모드 창으로 이동하는 방법

응용 프로그램으로 모드 창을 표시하면 t가 막히므로 주의해야 합니다.
이런 인상

           ⇩

처음에는 아래의 글을 참고하여 실현하였으나'present'에서 화면을 옮기려면 목적지의 VC를 옮길 수 없어 오류가 발생했습니다.
조사 결과 테이블 뷰에서 마이그레이션할 때 사용할 수 없기 때문에'오버라이드 func prepare'에서 마이그레이션 목적지 데이터를 준비한 뒤'perform Segue'로 마이그레이션하는 방법으로 변경됐다.
https://qiita.com/ngo275/items/80477dd74f77328eb4e0
ViewController.swift
//遷移元のVC

    func tableView(_ table: UITableView,didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "Modal", sender: nil)
    }

    // Segue 準備
    override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
        if (segue.identifier == "Modal") {
            guard let ModalVC = segue.destination as? ModalViewController else {
                return
            }

            ModalVC.modalPresentationStyle = .overCurrentContext
            ModalVC.view.backgroundColor = UIColor(red:0.94, green:0.94, blue:0.96, alpha:0.5)

        }
    }
ModalViewController.swift
//遷移先のVC

class ModalViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
//ここで閉じるボタン以外をタップした場合でも画面が閉じるように処理を記載
//tagで管理するとコードが書きやすい
        super.touchesEnded(touches, with: event)
        for touch: UITouch in touches {
            let tag = touch.view!.tag
            if tag == 1 {
                dismiss(animated: true, completion: nil)
            }
        }

    }

    @IBAction func didTapClose(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
기사 쓰는 방법을 잘 모르지만 매일 알찬 부분만 쓰면 상당히 많은 지식을 정리할 수 있다!!
힘내겠습니다.

좋은 웹페이지 즐겨찾기