iOS

[ IOS ] ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€๊ฐ€ ๋œจ๋Š” ์•Œ๋žŒ ์‹œ๊ณ„ ๋งŒ๋“ค๊ธฐ - ์–ผ๋Ÿฟ(Alert)

Forest Yun 2021. 7. 27. 22:38
728x90

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

๋ฌธ์ œ

ํ˜„์žฌ ์‹œ๊ฐ„์ด ์„ ํƒํ•œ ์‹œ๊ฐ„๊ณผ ๊ฐ™์•„์ง€๋ฉด ์–ผ๋Ÿฟ์œผ๋กœ ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์•Œ๋žŒ ์‹œ๊ณ„๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”.

์•Œ๋ฆผ ๋ฉ”์„ธ์ง€๋ฅผ ๋ˆ„๋ฅธ ํ›„์—๋Š” 1๋ถ„ ๋™์•ˆ ์•Œ๋ฆผ ์ฐฝ์ด ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๊ฒŒ ์„ค์ •ํ•ด ๋ณด์„ธ์š”.

 

 

๊ตฌํ˜„ ์ˆœ์„œ

  1. ๋ ˆ์ด์•„์›ƒ ๊ตฌ์„ฑ
  2. ์•„์šธ๋ › ๋ณ€์ˆ˜์™€ ์•ก์…˜ ํ•จ์ˆ˜ ์ƒ์„ฑ
  3. ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ - 1์ดˆ๋งˆ๋‹ค ํ˜„์žฌ ์‹œ๊ฐ„ ๊ฐฑ์‹ (ํƒ€์ด๋จธ ์‚ฌ์šฉ)
  4. ์„ ํƒ ์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ
  5. ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ์„ ํƒ ์‹œ๊ฐ„ ๋น„๊ต ํ›„ ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€ ๋„์šฐ๊ธฐ
  6. ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€ ํ™•์ธ ์งํ›„ 1๋ถ„ ๋™์•ˆ ์•Œ๋ฆผ ์ฐฝ์ด ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ

 

์ตœ์ข… ๊ฒฐ๊ณผ

 

 

 

 


https://88yhtserof.tistory.com/20

 

[ IOS ] ์•Œ๋žŒ ์‹œ๊ณ„ ๋งŒ๋“ค๊ธฐ - ๋ฐ์ดํ„ฐ ํ”ผ์ปค ์‚ฌ์šฉํ•ด ๋‚ ์งœ ์„ ํƒํ•˜๊ธฐ

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ ๋ฐ์ดํ„ฐ ํ”ผ์ปค๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹œ๊ฐ„์„ ์„ ํƒํ•˜๊ณ , ์„ ํƒ ์‹œ๊ฐ„ 1๋ถ„ ์ „๋ถ€ํ„ฐ ํ™”๋ฉด์ด ๋นจ๊ฐ„์ƒ‰๋˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. ์„ ํƒํ•œ ์‹œ๊ฐ„์ด ๋˜๋ฉด ๋ฐฐ๊ฒฝ์ƒ‰์„ ์›๋ž˜ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฐ๋‹ค. ๊ตฌ

88yhtserof.tistory.com

Date Picker ๋ฅผ ์‚ฌ์šฉํ•œ ์•Œ๋žŒ ์‹œ๊ณ„์— Alert ๊ธฐ๋Šฅ์„ ๋”ํ•ด ์•Œ๋žŒ ๋ฉ”์„ธ์ง€๊ฐ€ ๋œจ๋Š” ์•Œ๋žŒ ์‹œ๊ณ„๋ฅผ ๋งŒ๋“ค ์˜ˆ์ •์ด๋‹ค.

์•Œ๋žŒ ์‹œ๊ณ„ ๊ด€๋ จํ•œ ๋‚ด์šฉ์€ ์ด์ „ ๊ฒŒ์‹œ๊ธ€๊ณผ ๋™์ผํ•˜๋‹ค.

 

 

 

 

 


์•„์šธ๋ › ๋ณ€์ˆ˜์™€ ์•ก์…˜ ํ•จ์ˆ˜

  • ์•ก์…˜ ํ•จ์ˆ˜: ๊ฐ์ฒด๊ฐ€ ์„ ํƒ๋˜์—ˆ์„ ๋•Œ ์–ด๋–ค ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์•ก์…˜ํ•จ์ˆ˜๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.  ์˜ˆ) DatePicker์„ ์„ ํƒํ–ˆ์„ ๋•Œ 
  • ์•„์šธ๋ › ๋ณ€์ˆ˜: ๊ฐ์ฒด์˜ ๊ฐ’์„ ์ด์šฉํ•˜๊ฑฐ๋‚˜ ์†์„ฑ๋“ค์„ ์ œ์–ดํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์•„์šธ๋ › ๋ณ€์ˆ˜ ํ˜•ํƒœ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค. ์˜ˆ) ํ•ด๋‹น ๋ ˆ์ด๋ธ”์˜ text ๊ฐ’์ด ํ•„์š”ํ•  ๋•Œ

 

 

 

 

 

 


5. ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ์„ ํƒ ์‹œ๊ฐ„ ๋น„๊ต ํ›„ ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€ ๋„์šฐ๊ธฐ

 

์–ผ๋Ÿฟ Alert

: ์‚ฌ์šฉ์ž์—๊ฒŒ ์ค‘์š”ํ•œ ๋ฉ”์„ธ์ง€๋‚˜ ๊ฒฝ๊ณ  ๋ฌธ๊ตฌ๋ฅผ ๋‚˜ํƒ€๋‚ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ฃผ์˜๋ฅผ ์ง‘์ค‘์‹œํ‚ค๋Š” ๊ฒฝ๊ณ ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , ํ›„์† ์กฐ์น˜๋ฅผ ์ทจํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

UIAlertController

An object that displays an alert message to the user. 
Use this class to configure alerts and action sheets with the message that you want to display and the actions from which to choose. After configuring the alert controller with the actions and style you want, present it using the present(_:animated:completion:) method. 

์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฒฝ๊ณ  ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด์—ฌ์ค„ ๊ฐ์ฒด์ด๋‹ค.

์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œ์‹œํ•  ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€์™€ ์„ ํƒํ•  ๋™์ž‘๋“ค์— ๋Œ€ํ•œ ์•ก์…˜ ์‹œํŠธ๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์›ํ•˜๋Š” ๋™์ž‘๊ณผ alert ์Šคํƒ€์ผ๋กœ Alert Controller์„ ๊ตฌ์„ฑํ•œ ํ›„์— present(_:animated:completion:) ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ Alert๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

addAction(_ action: UIAlertAction)์„ ์‚ฌ์šฉํ•ด Alert์— ๋™์ž‘๊ณผ ์•ก์…˜ ์‹œํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

UIAlertAction

An action that can be taken when the user taps a button in an alert. 
You use this class to configure information about a single action, including the title to display in the button, any styling information, and a handler to execute when the user taps the button.

 

Alert์— ์žˆ๋Š” ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ทจํ•  ๋™์ž‘์ด๋‹ค. 

์ด ํด๋ž˜์Šค๋Š” title, style, handler๋ฅผ ํฌํ•จํ•˜์—ฌ ํ•˜๋‚˜์˜ ์•ก์…˜์„ ๊ตฌ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. handler๋Š” ์œ ์ €๊ฐ€ ๋ฒ„ํŠผ์„ ์„ ํƒํ•  ๋•Œ ์‹คํ–‰๋  ๋™์ž‘์ด๋‹ค. handler๋Š” ์ต๋ช…ํ•จ์ˆ˜๋กœ ์ž‘์„ฑ๋˜๋ฉฐ handler: {} ํ˜•์‹ ๋˜๋Š” UIAlertAction(title:style:){} ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

let alarmAlert = UIAlertController(title: "์•Œ๋ฆผ",
                                  message: "์„ค์ •๋œ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.",
                                  preferredStyle: UIAlertController.Style.alert)
                
let alarmAction1 = UIAlertAction(title: "ํ™•์ธ",
                               style: UIAlertAction.Style.default,
                               handler: {
                               ๋งค๊ฐœ๋ณ€์ˆ˜ in
                               ์‹คํ–‰ํ•  ์ฝ”๋“œ ์ž‘์„ฑ})
                               
let alarmAction2 = UIAlertAction(title: "์ทจ์†Œ",
                               style: UIAlertAction.Style.default,
                               handler: nil)
                               
                
alarmAlert.addAction(alarmAction1)
alarmAlert.addAction(alarmAction2)
present(alamAlert, animated: true, completion: nil)

 

 

 

 

 

 

 

 

 


6. ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€ ํ™•์ธ ์งํ›„ 1๋ถ„ ๋™์•ˆ ์•Œ๋ฆผ ์ฐฝ์ด ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ

์•Œ๋ฆผ ์—ฌ๋ถ€๋ฅผ Bool ํƒ€์ž…์œผ๋กœ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ false๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์„ ํƒํ•œ ์‹œ๊ฐ„๊ณผ ํ˜„์žฌ ์‹œ๊ฐ„์ด ๊ฐ™๊ณ  ์•Œ๋ฆผ ์—ฌ๋ถ€๊ฐ€ false์ผ ๋•Œ ์•Œ๋ฆผ ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋‚ด๊ฒŒ ๋˜๋ฉด true๋กœ ๋ณ€๊ฒฝํ•˜๊ณ , ์„ ํƒํ•œ ์‹œ๊ฐ„๊ณผ ํ˜„์žฌ ์‹œ๊ฐ„์ด ๊ฐ™์ง€ ์•Š์„ ๋•Œ๋Š” false๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. 

์„ ํƒํ•œ ์‹œ๊ฐ„์ด ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฑด 1๋ถ„์ด ์ง€๋‚ฌ๋‹ค๋Š” ์˜๋ฏธ๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์„ ํƒํ•œ ์‹œ๊ฐ„์ด ํ๋ฅด๋Š” 1๋ถ„ ๋™์•ˆ ์•Œ๋ฆผ ์ฐฝ์ด ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

import UIKit

class AlertMissionViewController: UIViewController {

    var alarmFlag:Bool = false //์•Œ๋žŒ ์—ฌ๋ถ€
    
    
    ...
    
    
    
    @objc func updateTime(){
        let date: NSDate = NSDate() //ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋ฐ›์•„์˜ค๋Š” ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ
        var currentTime:String = String()
        let formatter = DateFormatter()
        
        formatter.dateFormat = "yyyy๋…„MM์›”dd์ผ a hh์‹œmm๋ถ„ss์ดˆ"
        lblCurrentTime.text = formatter.string(from: date as Date) //ํ˜„์žฌ์‹œ๊ฐ„ ์ถœ๋ ฅ
        
        formatter.dateFormat = "yyyy๋…„MM์›”dd์ผ a hh์‹œmm๋ถ„"
        currentTime = formatter.string(from: date as Date)
        
        if alamTime == currentTime {
            if !alarmFlag { //์•Œ๋žŒ์ด ์•„์ง ์šธ๋ฆฌ์ง€ ์•Š์•˜๋‹ค๋ฉด
                let alamAlert = UIAlertController(title: "์•Œ๋ฆผ",
                                                  message: "์„ค์ •๋œ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.",
                                                  preferredStyle: UIAlertController.Style.alert)
                
                let alamAction = UIAlertAction(title: "ํ™•์ธ",
                                               style: UIAlertAction.Style.default,
                                               handler: nil)
                
                alamAlert.addAction(alamAction)
                present(alamAlert, animated: true, completion: nil)
                
                alarmFlag = true
            }
        }
        else { //ํ˜„์žฌ ์‹œ๊ฐ„์ด ์„ ํƒํ•œ ์‹œ๊ฐ„๊ณผ ๋‹ค๋ฅด๋‹ค๋ฉด
            alarmFlag = false
        }
        //ํ˜„์žฌ ์‹œ๊ฐ„์ด ์„ ํƒํ•œ ์‹œ๊ฐ„๊ณผ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฑด 1๋ถ„์ด ์ง€๋‚ฌ๋‹ค๋Š” ์˜๋ฏธ - 1๋ถ„ ๋™์•ˆ ์•Œ๋ฆผ ์ฐฝ์ด ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค
        //์•Œ๋žŒ ์‹œ๊ฐ„์ด ์•„๋‹ ๋•Œ ์•Œ๋žŒ ์—ฌ๋ถ€๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค.
    }
}

 

 

 

 

 

 

 


์ „์ฒด ์ฝ”๋“œ

import UIKit

class AlertMissionViewController: UIViewController {
    
    let timeSelector: Selector = #selector(AlertMissionViewController.updateTime)//ํƒ€์ด๋จธ๊ฐ€ ๊ตฌ๋™๋˜๋ฉด ํ˜ธ์ถœํ•  ํ•จ์ˆ˜ ์ง€์ •
    let interval:Double = 1.0 //ํ˜„์žฌ์‹œ๊ฐ„์ด ๊ฐฑ์‹ ๋  ๊ฐ„๊ฒฉ
    var alarmTime:String = String()
    var alarmFlag:Bool = false //์•Œ๋žŒ ์—ฌ๋ถ€
    
    @IBOutlet var lblCurrentTime: UILabel!
    @IBOutlet var lblSelectTime: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //ํ˜„์žฌ์‹œ๊ฐ„์„ interval๋งˆ๋‹ค ๊ฐฑ์‹ ์‹œ์ผœ ์ค„ ํƒ€์ด๋จธ ์„ค์ •
        Timer.scheduledTimer(timeInterval: interval, target: self, selector: timeSelector, userInfo: nil, repeats: true)
    }
    
    //๋ฐ์ดํŠธํ”ผ์ปค๋ทฐ๊ฐ€ ๋™์ž‘ํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ์•ก์…˜ํ•จ์ˆ˜
    @IBAction func changeDatePicker(_ sender: UIDatePicker) {
        let selectDate = sender.date
        
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy๋…„MM์›”dd์ผ a hh์‹œmm๋ถ„"
        
        lblSelectTime.text = formatter.string(from: selectDate)
        alarmTime = formatter.string(from: selectDate)
    }
    
    @objc func updateTime(){
        let date: NSDate = NSDate() //ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋ฐ›์•„์˜ค๋Š” ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ
        var currentTime:String = String()
        let formatter = DateFormatter()
        
        formatter.dateFormat = "yyyy๋…„MM์›”dd์ผ a hh์‹œmm๋ถ„ss์ดˆ"
        lblCurrentTime.text = formatter.string(from: date as Date) //ํ˜„์žฌ์‹œ๊ฐ„ ์ถœ๋ ฅ
        
        formatter.dateFormat = "yyyy๋…„MM์›”dd์ผ a hh์‹œmm๋ถ„"
        currentTime = formatter.string(from: date as Date)
        
        if alarmTime == currentTime {
            if !alarmFlag { //์•Œ๋žŒ์ด ์•„์ง ์šธ๋ฆฌ์ง€ ์•Š์•˜๋‹ค๋ฉด
                let alarmAlert = UIAlertController(title: "์•Œ๋ฆผ",
                                                  message: "์„ค์ •๋œ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.",
                                                  preferredStyle: UIAlertController.Style.alert)
                
                let alarmAction = UIAlertAction(title: "ํ™•์ธ",
                                               style: UIAlertAction.Style.default,
                                               handler: nil)
                
                alarmAlert.addAction(alarmAction)
                present(alarmAlert, animated: true, completion: nil)
                
                alarmFlag = true
            }
        }
        else { //ํ˜„์žฌ ์‹œ๊ฐ„์ด ์„ ํƒํ•œ ์‹œ๊ฐ„๊ณผ ๋‹ค๋ฅด๋‹ค๋ฉด
            alarmFlag = false
        }
        //ํ˜„์žฌ ์‹œ๊ฐ„์ด ์„ ํƒํ•œ ์‹œ๊ฐ„๊ณผ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฑด 1๋ถ„์ด ์ง€๋‚ฌ๋‹ค๋Š” ์˜๋ฏธ - 1๋ถ„ ๋™์•ˆ ์•Œ๋ฆผ ์ฐฝ์ด ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค
        //์•Œ๋žŒ ์‹œ๊ฐ„์ด ์•„๋‹ ๋•Œ ์•Œ๋žŒ ์—ฌ๋ถ€๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค.
    }
}

 

 

 

 

 

 

 

 

๊ณต๋ถ€์ž๋ฃŒ

Do It! ์Šค์œ„ํ”„ํŠธ๋กœ ์•„์ดํฐ ์•ฑ ๋งŒ๋“ค๊ธฐ ๊ฐœ์ • 5ํŒ
SWIFT ๋ฌธ์„œ
728x90