개발자 코드(Code)/Swfit

IOS) 경고 알림창 띄우기 기능 + 한 번에 3개의 기능 구현하기

Chain X 2020. 8. 26. 13:41
728x90
반응형

IOS 경고 알림창 띄우기 기능 + 한 번에 3개의 기능 구현하기

 

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

반응형