본문 바로가기

iOS 앱 개발자 프로젝트/iOS 입문

[iOS] 간단한 카운트 화면 만들기

2024년 3월 25일 월요일

 

버튼을 클릭하면 숫자가 증가하거나 감소하는 UI 화면을 만들고

IBAction과 IBOutlet을 사용해 보기


 

 

레이아웃 요구사항

  1. UILabel이 가운데 위치하게 해 주세요.
  2. UILabel을 기준으로 상단에는 감소 버튼, 아래에는 증가 버튼을 위치시켜 주세요.
  3. UILabel과 UIButton사이의 간격은 16px로 설정해 주세요.
  4. AutoLayout을 사용해 주세요.

 

로직 요구사항

  1. count의 시작은 0으로 시작해 주세요.
  2. 감소버튼을 눌렀을 때, -1씩 감소시켜 UILabel에 표시해 주세요.
  3. 증가버튼을 눌렀을 때, +1씩 증가시켜 UILabel에 표시해 주세요.

 

//
//  ViewController.swift
//  simpleCount
//
//  Created by 채나연 on 3/25/24.
//

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var textLabel: UILabel!
    private var count: Int = 0
 
    @IBAction func tappedDecreaseButton(_ sender: UIButton) {
        self.count -= 1
        self.refreshTextLabel()
    }
    

    @IBAction func tappedIncreaseButton(_ sender: UIButton) {
        self.count += 1
        self.refreshTextLabel()
    }
    

    private func refreshTextLabel() {
        self.textLabel.text = String(self.count)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.refreshTextLabel()
        // Do any additional setup after loading the view.
    }


}

 

 

  빌드를 하면 아무런 오류가 나지 않았는데 아래와 같은 에러가 떠서 뭔가 싶었는데..

 

 

📌 this class is not key value coding-comliant for the key

 

Outlets 연결해 주었을 때,

코드 상에서 이름을 바꾸거나 연결을 해제해 주었을 때는 꼭

Connections Inspector에서 변경사항을 반영해야 한다.

 

 

vc 에서 코드를 수정해서 해결되는 오류가 아니라 main storyboard의 Inspector에서 해결해야하는 오류도 있다.