ViewController.swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let nextViewController: SecondViewController =
segue.destination as? SecondViewController else {
return
}
guard let cell: UITableViewCell = sender as? UITableViewCell else {
return
}
nextViewController.textToSet = cell.textLabel?.text
}
ViewController에서 SecondViewController로 데이터를 넘겨줄 때 사용한다.
guard let으로 받아오지 못하면 바로 종료
as? 로 타입캐스팅
nextViewController = 다음 화면
SecondViewController.swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.textLabel.text = self.textToSet
}
다음 화면이 나타나기 전에 값을 넣어주기
SecondViewController에서 세팅을 해준 이융
prepare 함수는 nextViewController를 준비하는 과정이기 때문에
View의 요소들은 아직 메모리에 올라와있지 않다고 한다.
그래서 textLabel에 직접 값을 넣어주려고하면 textLabel이 아직 메모리에 올라와 있지 않기때문에
exception (오류)가 발생한다.
Segue가 여러개일 경우
segue의 identifier로 구분할 수 있다.
prepare 함수안에 segue.identifier
'iOS > STUDY' 카테고리의 다른 글
[iOS] scrollview autolayout (0) | 2021.03.31 |
---|---|
[iOS] The data couldn’t be read because it is missing. (1) | 2021.03.20 |
[iOS] UITableView (0) | 2021.03.15 |
[iOS] Singleton (3) | 2021.03.12 |
[iOS] UITextField 암호화 시 Strong Password (0) | 2021.03.08 |