728x90
반응형
//
// ViewController.swift
// Alert
//
// Created by Leo_Jang on 25/08/2020.
// Copyright © 2020 Leo_Jang. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var lampImage: UIImageView!
let imgOn = UIImage(named: "lamp_on.png")
let imgOff = UIImage(named: "lamp_off.png")
let imgRemove = UIImage(named: "lamp_remove.png")
// 램프가 켜져 있는지 커저 있는지 확인하는 변수가 필요하다.
var isLampOn = true // Defalut 값
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// 초기값 설정
lampImage.image = imgOn
}
@IBAction func btnLampOn(_ sender: UIButton) {
if isLampOn {
// 컨트롤러 부분 (상단 부분)
let lampOnAlert = UIAlertController(title: "경고!", message: "현재 램프가 ON 상태 입니다.", preferredStyle: UIAlertController.Style.alert)
let onAction = UIAlertAction(title: "네", style: UIAlertAction.Style.default, handler: nil)
lampOnAlert.addAction(onAction)
// present 메소드를 이용하면 기존 뷰컨트롤러위에 새로운 뷰컨트롤러를 덮어 씌어 참조하는 관계를 형성. 즉 present가 없으면 창이 화면뒤에 뜬다. 앞으로 보내주기 위해서는 이걸 써야한다.
present(lampOnAlert, animated: true, completion: nil)
}else{
// 램프가 안켜져 있으면 켜져있다고 값을 준다.
lampImage.image = imgOn
isLampOn = true // 켜져있다는 값을 준다. (기준값)
}
}
@IBAction func btnLampOff(_ sender: UIButton) {
// 램프끄기
if isLampOn {
let lampOffAlert = UIAlertController(title: "램프 끄기", message: "램프를 끄시겠습니까?", preferredStyle: UIAlertController.Style.alert)
//Closure를 이용한 실행
let offAction = UIAlertAction(title: "네", style: UIAlertAction.Style.default, handler: {ACTION in
self.lampImage.image = self.imgOff
self.isLampOn = false
})
let cancelAtion = UIAlertAction(title: "아니요", style: UIAlertAction.Style.default, handler: nil)
lampOffAlert.addAction(offAction)
lampOffAlert.addAction(cancelAtion)
present(lampOffAlert, animated: true, completion: nil)
}else{ //꺼져 있는데 끄겠다고 할때
let lampOffAlert = UIAlertController(title: "경고!", message: "현재 OFF 상태 입니다.", preferredStyle: UIAlertController.Style.alert)
let offAction = UIAlertAction(title: "네", style: UIAlertAction.Style.default , handler: nil)
lampOffAlert.addAction(offAction)
present(lampOffAlert,animated: true, completion: nil)
}
}
@IBAction func btnLampRemove(_ sender: UIButton) {
// 램프제거? 램프를 제거 할까요? // 아니요.끕니다./ 아니요. 켭니다. / 네.제거합니다. //컨트롤러와 액션 3개
let lampRemoveAlert = UIAlertController(title: "경고!", message: "램프를 제거 할까요?", preferredStyle: UIAlertController.Style.alert)
let offAction = UIAlertAction(title: "아니요 램프를 꺼주세요!.", style: UIAlertAction.Style.default, handler:
{ACTION in
self.lampImage.image = self.imgOff
self.isLampOn = false})
let onAction = UIAlertAction(title: "아니요 램프를 켜주세요!", style: UIAlertAction.Style.default, handler:
{Action in
self.lampImage.image = self.imgOn
self.isLampOn = true})
let RemoveAction = UIAlertAction(title: "네 제거하겠습니다.", style: UIAlertAction.Style.default, handler:
{Action in
self.lampImage.image = self.imgRemove
self.isLampOn = false})
lampRemoveAlert.addAction(offAction)
lampRemoveAlert.addAction(onAction)
lampRemoveAlert.addAction(RemoveAction)
present(lampRemoveAlert, animated: true, completion: nil)
}
} // ------------
반응형
'개발자 코드(Code) > Swfit' 카테고리의 다른 글
IOS ) WKWebView를 통해 JSP 연결 (아파치 톰캣 필요)(사진필요) (0) | 2020.08.26 |
---|---|
IOS) PageControl + label (홀수 :빨간색 , 짝수 : 파란색) (0) | 2020.08.26 |
IOS) PickerDate Quiz 빨간색 + 파란색 (0) | 2020.08.26 |
IOS) Picker date + 라벨(현재 시간, 사용자가 선택한 시간) (0) | 2020.08.26 |
IOS) Button (이전+다음), Label (파일명) , ImageView(배열을 통한 이미지 삽입) (0) | 2020.08.26 |