[ 2022-08-23 ๐Ÿ˜ฝ TIL ]

Burkeyยท2022๋…„ 8์›” 23์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
20/157
post-thumbnail

์˜ค๋Š˜์˜ ์ง„์ฒ™๋„

์ค‘์š”๋„ ์„ ํƒ์„ ํ•˜๋Š”๋ฐ dropDown์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์–ด์„œ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.
cocoaPod๋ฅผ ์ด์šฉํ•˜์—ฌ DropDown๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„ ์ ์šฉํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
(cocoaPod๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ฒ˜์Œ ์„ค์น˜ํ•ด๋ด์„œ ๋งŽ์ด ํ•ด๋งธ์Šต๋‹ˆ๋‹ค.)

๋ง‰์ƒ ์ ์šฉํ•ด๋ณด๋‹ˆ ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ๋ณ€ํ•œ๊ฒƒ์€ ์—†์ง€๋งŒ
๊ทธ๋ž˜๋„ ์ด๋ฒˆ์— Pod๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ ๋ฐฐ์šด ๊ฒƒ ๊ฐ™์•„ ๊ธฐ๋ถ„์€ ์ข‹์Šต๋‹ˆ๋‹ค.

import UIKit
import DropDown

//todos.append([
//    "todo" : "๋ฐฅ๋จน๊ธฐ",
//    "start" : "2022-08-12",
//    "end" : "2022-08-14",
//    "ReDays" : ["์›”"],
//    "alarms" : false ,
//    "important" : "์ƒ",
//    "notes" : "๋ฐฅ๋จน๊ณ  ์ž˜์ž๊ธฐ",
//    "done" : false
//])//ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์„ค์ •

class AddViewController: UIViewController {
    @IBOutlet var tdToDo: UITextField!
    @IBOutlet var tfMemo: UITextField!
    @IBOutlet var btnImportant: UIButton!
    
    var todo = Dictionary<String, Any>()
    
    var reDays = [String]()
    var alarmSet = false
    var startDay : String? = nil
    var endDay : String? = nil
    var important : String? = nil
    let menu = DropDown()
    //๋“œ๋กญ๋‹ค์šด ์‚ฌ์šฉ
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        menu.dataSource = [ "์ƒ", "์ค‘", "ํ•˜" ]//DropDown๋ฆฌ์ŠคํŠธ ์ง€์ •
        menu.anchorView = btnImportant
        //๋“œ๋กญ๋‹ค์šด ๋ฐ•์Šค์˜ ์œ„์น˜๋ฅผ ํ™”๋ฉด ์ค‘์•™์—์„œ ๋ฒ„ํŠผ์œ„์น˜๋กœ ๋ณ€๊ฒฝ
        menu.bottomOffset = CGPoint(x: 0, y: btnImportant.bounds.height)
        //๋“œ๋กญ๋‹ค์šด ๋ฐ•์Šค์˜ ์œ„์น˜๋ฅผ ํ™”๋ฉด ์ค‘์•™์—์„œ ๋ฒ„ํŠผ์˜ ์•„๋ž˜๋กœ ์œ„์น˜ ๋ณ€๊ฒฝ
        menu.selectionAction = { index, title in
            self.important = title
            self.btnImportant.setTitle(title, for: UIControl.State.normal)
            //Item ๋ˆ„๋ฅผ์‹œ์— ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์ €์žฅ
        }
    }

    @IBAction func choiceStartDay(_ sender: UIDatePicker) {
        let datePK = sender
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        
        startDay = dateFormatter.string(from: datePK.date)
    }
    
    @IBAction func choiceEndDay(_ sender: UIDatePicker) {
        let datePK = sender
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        
        endDay = dateFormatter.string(from: datePK.date)
    }
    
    @IBAction func toggleAlarm(_ sender: UISwitch) {
        alarmSet = sender.isOn
    }
    
    @IBAction func addTodo(_ sender: UIButton) {
        if (checkInputValue(tdToDo.text) && checkInputValue(startDay)
        && checkInputValue(endDay)) {
            todo["todo"] = tdToDo.text
            todo["start"] = startDay
            todo["end"] = endDay
            todo["alarms"] = alarmSet
            todo["important"] = important
            todo["notes"] = tfMemo.text
            todo["done"] = false
            //์„œ๋ฒ„์— ์ „์†ก
        }else{
            let alert = UIAlertController(title: "์ž…๋ ฅ๊ฐ’ ํ™•์ธ", message: "๋‚ด์šฉ์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”", preferredStyle: UIAlertController.Style.alert)
            let alertAction = UIAlertAction(title: "ํ™•์ธ", style: UIAlertAction.Style.cancel, handler: nil)
            
            alert.addAction(alertAction)
            self.present(alert, animated: true, completion: nil)
        }
        print("todo: " , todo)
    }
    
    @IBAction func cancelTodo(_ sender: Any) {
        startDay = nil
        endDay = nil
        
        //todo ๋ชฉ๋ก์œผ๋กœ ๊ฐ€๊ธฐ
    }
    
    func checkInputValue(_ val : String?) -> Bool {
        if (val == nil) || (val  == "") {
            return false
        }
        return true
    }

    @IBAction func btnChangeImportant(_ sender: UIButton) {
        menu.show()
        print("click")
        //๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ์‹œ์— DropDowm์„ ๋ณด์—ฌ์คŒ 
    }
}

๋„์›€์„ ์ค€ ์ž๋ฃŒ :
์˜์ƒ ์ž๋ฃŒ ๊ตฟ๊ตฟ
ํ•œ๊ธ€ ์ž๋ฃŒ ๊ตฟ๊ตฟ

profile
์Šคํƒฏ ์˜ฌ๋ฆฌ๋Š” ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€