버튼 관련 코드

Swift 2021. 2. 24. 22:34
import UIKit

class ViewController: UIViewController {

    var currentValue = 0
    
    @IBOutlet weak var PriceLabel: UILabel!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        Refresh()
    }
    
    
    @IBAction func Refresh(_ sender: Any) {
        
        let message = "가격은 \(currentValue)입니다"
        
        // 버튼 눌렀을 때 팝업 띄우는 코드
        let alert = UIAlertController(title: "Hello", message: message, preferredStyle: .alert)
        
        // ok 버튼을 누르면 변경된 가격이 바로 화면에 나옴
        let action = UIAlertAction(title: "OK", style: .default, handler: { action in self.Refresh()})
        alert.addAction(action)
        
        // present를 통해서 alert을 띄움
        present(alert, animated: true, completion: nil)
        
    }
    
    // Refresh 버튼을 눌렀을 때 가격 변경
    func Refresh() {
        
        // 1부터 10000까지의 숫자를 랜덤으로 숫자를 가져옴
        let randomPrice = arc4random_uniform(10000) + 1
        
        // randomPrice는 UIntt32, current Value는 Int 타입이라 오류
        // randomPirce를 Int형으로 변환
        currentValue = Int(randomPrice)
        PriceLabel.text = "$\(currentValue)"
    }
}

'Swift' 카테고리의 다른 글

MVVM 디자인 패턴  (0) 2021.03.04
Table View  (0) 2021.03.03
채팅 앱 구조의 이해와 테이블 뷰 활용  (0) 2021.02.18
로그인 화면 동적 변환 구조  (0) 2021.02.17
팝업 레이아웃  (0) 2021.02.16
Posted by khon98
,