5월 5일 (목)

apwierk·2022년 5월 6일
0

TIL

목록 보기
12/33
post-thumbnail

TIL (Today I Learned)

5월 5일 (목)

학습 내용

1. 프로퍼티를 이용해서 데이터 주고받기(A->B)

  • 데이터를 보내는 곳
class FirstViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    @IBAction func changeLabel(_ sender: UIButton) {
        guard let vc = storyboard?.instantiateViewController(identifier: "SecondViewController")
        as? SecondViewController else { return }
        vc.text = "data Passing" // 보내는 데이터
        navigationController?.pushViewController(vc, animated: true) // 보냄
    }
}
  • 데이터를 받는 곳
class SecondViewController: UIViewController {
    var text: String = ""
    @IBOutlet weak var passingInfo: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    @IBAction func changeLabel(_ sender: UIButton) {
        passingInfo?.text = text
    }
}

문제점 / 해결한 점

그림과 같이 identifier를 설정하지 않아 오류가 났었다.
Class 이외에도 Storyboard ID를 같게 설정해 주어야 연결이 되는 것을 알게 되었다.


일기

오래 앉아 있어서 그런지 기력이 떨어져 비타민을 먹기로 했다.
몸 챙겨 먹고 살자.

profile
iOS 꿈나무 개발자

0개의 댓글