ViewController 역할
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func hello(_ sender: Any) {
let alert = UIAlertController(title: "Hello", message: "My Frist App!!", preferredStyle: .alert) // UIAlertController으로 팝업을 만듬
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action) // Button은 UIAlertAction을 가지고 만들고
present(alert, animated: true, completion: nil) // present을 통해서 알럿을 띄운다
}
}
Button을 누르면 알럿이 나온며 OK 버튼을 누르면 알럿이 꺼진다
첫번째 : 오타가 발생 했을 때 (자동완성 기능을 사용해주면 좋다)
두번재 : 대소문자 구분
세번째 : 바운더리를 나가서 작성 안되록 주의
네번째 : 버튼이 연결된 함수가 맞는지 확인(다르면 앱이 죽는다)
다섯번째 : 변형되지 않는 변수는 var가 아닌 let으로 해야한다(애플에서 let을 추천한다)
View
모든 뷰 컴퍼넌트는 UIKi이라는 프레임 워크 안에 있다
스크린에 좌측 상단이 기준이다
X축은 오른쪽으로 갈 수 록 점점 커지고 Y축은 아래로 갈 수 록 점점 커진다
Odject는 특정 일을 수행 한다 Object의 구성은 데이터와 메소드로 구성되어 있다.
String and Variable
import UIKit
class ViewController: UIViewController {
var currentValue = 0
@IBOutlet weak var priceLable: UILabel!
// view에 있는 lable을 아울렛에 연결
override func viewDidLoad() {
super.viewDidLoad()
priceLable.text = "₩ \(currentValue)"
// text 업데이트
}
@IBAction func showAlert(_ sender: Any) {
let message = "가격은 \(currentValue) 입니다"
let alert = UIAlertController(title: "Bye", message: message, preferredStyle: .alert) // UIAlertController으로 팝업을 만듬
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action) // Button은 UIAlertAction을 가지고 만들고
present(alert, animated: true, completion: nil) // present을 통해서 알럿을 띄운다
let randomPrice = arc4random_uniform(10000) + 1
// 1 ~ 10000 사이에 랜덤 숫자를 생성
currentValue = Int(randomPrice)
priceLable.text = "₩ \(currentValue)"
// Refresh를 누를때마다 text 업데이트
}
}
var vs. let
var, let 차이점
값이 있는 상태에서 var는 안에 value는 변경할 수 있고 let은 변경할 수 없다
중복 코드가 있으면 메소드를 만들어 사용한다
import UIKit
class ViewController: UIViewController {
var currentValue = 0
@IBOutlet weak var priceLable: UILabel!
// view에 있는 lable을 아울렛에 연결
override func viewDidLoad() {
super.viewDidLoad()
let randomPrice = arc4random_uniform(10000) + 1
// 1 ~ 10000 사이에 랜덤 숫자를 생성
currentValue = Int(randomPrice)
priceLable.text = "₩ \(currentValue)"
// text 업데이트
}
@IBAction func showAlert(_ sender: Any) {
let message = "가격은 \(currentValue) 입니다"
let alert = UIAlertController(title: "Bye", message: message, preferredStyle: .alert) // UIAlertController으로 팝업을 만듬
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action) // Button은 UIAlertAction을 가지고 만들고
present(alert, animated: true, completion: nil) // present을 통해서 알럿을 띄운다
let randomPrice = arc4random_uniform(10000) + 1
// 1 ~ 10000 사이에 랜덤 숫자를 생성
currentValue = Int(randomPrice)
priceLable.text = "₩ \(currentValue)"
// Refresh를 누를때마다 text 업데이트
}
}
import UIKit
class ViewController: UIViewController {
var currentValue = 0
@IBOutlet weak var priceLable: UILabel!
// view에 있는 lable을 아울렛에 연결
override func viewDidLoad() {
super.viewDidLoad()
refresh()
}
@IBAction func showAlert(_ sender: Any) {
let message = "가격은 \(currentValue) 입니다"
let alert = UIAlertController(title: "Bye", message: message, preferredStyle: .alert) // UIAlertController으로 팝업을 만듬
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action) // Button은 UIAlertAction을 가지고 만들고
present(alert, animated: true, completion: nil) // present을 통해서 알럿을 띄운다
refresh()
}
func refresh() { // 중복 코드가 있으면 메소드를 만들어 사용한다
let randomPrice = arc4random_uniform(10000) + 1
// 1 ~ 10000 사이에 랜덤 숫자를 생성
currentValue = Int(randomPrice)
priceLable.text = "₩ \(currentValue)"
}
}
참고 : FastCampus iOS 앱 개발 올인원 패키지