Delegate๋ก ํน์ Cell์ ํด๋ฆญํ์ ๋ ์์ธ ์ ๋ณด ํ์ด์ง๋ก ์ด๋ํ๋๋ก ๊ตฌํํ๋ค.
extension ViewController : UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let viewController = self.storyboard?.instantiateViewController(identifier: "DiaryDetailViewController") as? DiaryDetailViewController else { return }
let diary = self.diaryList[indexPath.row]
viewController.diary = diary
viewController.indexPath = indexPath
self.navigationController?.pushViewController(viewController, animated: false)
}
}
์์ธ ์ ๋ณด ํ์ด์ง์ธ DiaryDetailViewController์์๋ ์ ๋ฌ๋ฐ์ diary ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ View๋ฅผ ์ด๊ธฐํํ๋ค.
//
// DiartyDetailViewController.swift
// Basic_05
//
// Created by ๊ณ ๋ on 2022/09/06.
//
import UIKit
class DiaryDetailViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var contentsTextView : UITextView!
@IBOutlet weak var dateLabel: UILabel!
var diary : Diary?
var indexPath : IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
self.configureView()
}
private func configureView() {
guard let diary = self.diary else { return }
self.titleLabel.text = diary.title
self.contentsTextView.text = diary.contents
self.dateLabel.text = self.dateToString(date: diary.date)
}
private func dateToString(date : Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yy๋
MM์ dd์ผ(EEEEE)"
formatter.locale = Locale(identifier: "ko_KR")
return formatter.string(from: date)
}
}
์ดํ ๋น๋๋ฅผ ์งํํ๋๋ฐ Cell์ ํด๋ฆญํ๋ฉด ์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉฐ ์ฑ์ด ์ค์ง๋๋ค.
Exception NSException * "-[UIView setText:]: unrecognized selector sent to instance 0x13110b480" 0x0000000283b5d920
Outlet๋ณ์์ View๊ฐ ์ ๋๋ก ์ฐ๊ฒฐ๋์ด ์์ง ์๋ค๋ ์๊ฐ์ ์คํ ๋ฆฌ๋ณด๋๋ฅผ ์ดํด๋ดค๋ค. Outlet๋ณ์์ ๋ง์ฐ์ค๋ฅผ ๊ฐ๋ค๋์ผ๋ฉด ์คํ ๋ฆฌ๋ณด๋ ์์์ ์ฐ๊ฒฐ๋ View์ ๋ํ ์ ๋ณด๋ฅผ ํ์ธํ ์ ์๋๋ฐ titleLabel์ UILabel์ด ์๋ UIView์ ์ฐ๊ฒฐ๋์ด ์๋ ๊ฒ์ ๋ฐ๊ฒฌํ ์ ์์๋ค.
titleLabel์ ๊ธฐ์กด ์ฐ๊ฒฐ์ ์ ๊ฑฐํ๊ณ ์ ์์ ์ผ๋ก ์ฐ๊ฒฐํด์ฃผ์๋ค.