[Swift] TableView에서 모드 창으로 이동하는 방법
이런 인상
⇩
처음에는 아래의 글을 참고하여 실현하였으나'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)
}
기사 쓰는 방법을 잘 모르지만 매일 알찬 부분만 쓰면 상당히 많은 지식을 정리할 수 있다!!힘내겠습니다.
Reference
이 문제에 관하여([Swift] TableView에서 모드 창으로 이동하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/goombeer/items/c5b3adaa176ca6585ae3텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)