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)"
}
}