[Extension] UIViewController + makeAlert ๐Ÿ’

su_veraยท2021๋…„ 11์›” 16์ผ

Swift Extension ๐Ÿฅ‘

๋ชฉ๋ก ๋ณด๊ธฐ
2/2

UIAlertController๋ฅผ ๋งŒ๋“œ๋Š” extension

๐ŸŽ parameters:
- title: ์•Œ๋ฆผ์ฐฝ์— ๋œจ๋Š” ํƒ€์ดํ‹€ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
- message: ํƒ€์ดํ‹€ ๋ฐ‘์— ๋œจ๋Š” ๋ฉ”์„ธ์ง€ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
- okAction: ํ™•์ธ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
- cancelAction: ์ทจ์†Œ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
- completion: ํ•ด๋‹น UIAlertController๊ฐ€ ๋„์›Œ์กŒ์„ ๋•Œ, ๋™์ž‘ํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.

extension UIViewController
{
   func makeRequestAlert(title : String,
                  message : String,
                  okAction : ((UIAlertAction) -> Void)?,
                  cancelAction : ((UIAlertAction) -> Void)? = nil,
                  completion : (() -> Void)? = nil)
   {
       
       let generator = UIImpactFeedbackGenerator(style: .medium)
       generator.impactOccurred()
       
       let alertViewController = UIAlertController(title: title, message: message,
                                                   preferredStyle: .alert)
       
       let okAction = UIAlertAction(title: "ํ™•์ธ", style: .default, handler: okAction)
       alertViewController.addAction(okAction)
       
       
       let cancelAction = UIAlertAction(title: "์ทจ์†Œ", style: .cancel, handler: cancelAction)
       alertViewController.addAction(cancelAction)
       

       self.present(alertViewController, animated: true, completion: completion)
   }
   
   func makeAlert(title : String,
                  message : String,
                  okAction : ((UIAlertAction) -> Void)? = nil,
                  completion : (() -> Void)? = nil)
   {
       let generator = UIImpactFeedbackGenerator(style: .medium)
       generator.impactOccurred()
       
       let alertViewController = UIAlertController(title: title, message: message,
                                                   preferredStyle: .alert)
       
       let okAction = UIAlertAction(title: "ํ™•์ธ", style: .default, handler: okAction)
       alertViewController.addAction(okAction)
       
       
       self.present(alertViewController, animated: true, completion: completion)
   }
   
profile
iOS ๊ฐœ๋ฐœ์ผ๊ธฐ โœจ

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