์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[iOS] ๊ธฐ๋ณธ AlertView ์‚ฌ์šฉํ•˜๊ธฐ 1

iOS ๐ŸŽ

by rangoo_ 2021. 2. 21. 17:02

๋ณธ๋ฌธ

์•ˆ๋…•ํ•˜์„ธ์š”! ์•„์ดํฐ์„ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํŒ์—…์ฐฝ ๋งŽ์ด ๋ณด์‹คํ…๋ฐ์š”!

์˜ค๋Š˜์€ iOS UIAlertController๋ฅผ ์‚ฌ์šฉํ•ด์„œ AlertView๋ฅผ ๋„์šฐ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


1. Alert View ๊ตฌ์„ฑ ์š”์†Œ


2. Alert View ๋งŒ๋“œ๋Š” ์ˆœ์„œ

๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์•Œ์•„๋ดค์œผ๋‹ˆ Xcode ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ง์ ‘ Alert View๋ฅผ ๋„์›Œ๋ด…์‹œ๋‹ค!

์šฐ์„  ๊ฐ„๋‹จํ•˜๊ฒŒ ์Šคํ† ๋ฆฌ๋ณด๋“œ์— ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด Alert View๋ฅผ ๋„์šธ ์ˆ˜ ์žˆ๋„๋ก ๋ฒ„ํŠผ์˜ IBAction ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์งœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

โ‘  AlertController

AlertController์—์„œ ์ง€์ •ํ•  ๋ถ€๋ถ„์€ ์—ฌ๊ธฐ์ž…๋‹ˆ๋‹ค

Alert์˜ ๊ตฌ์„ฑ์š”์†Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค

  • Title: alert์˜ ์ œ๋ชฉ

  • Message: alert์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์ œ๊ณตํ•˜๋Š” ์„ค๋ช… ํ…์ŠคํŠธ

  • preferredStyle: alert ์ปจํŠธ๋กค๋Ÿฌ์˜ ์Šคํƒ€์ผ - alert, action sheet ๋‘ ์ข…๋ฃŒ๊ฐ€ ์žˆ๋Š”๋ฐ ์˜ค๋Š˜ ์‚ฌ์šฉํ•  ์Šคํƒ€์ผ์€ alert์ž…๋‹ˆ๋‹ค

๊ฐ ๊ตฌ์„ฑ์š”์†Œ์— ๋งž๋Š” ๊ฐ’์„ ์ฐจ๋ก€๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ title์€ ๋ณผ๋“œ, message๋Š” ๊ธฐ๋ณธ ์„œ์ฒด๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค

        let alert = UIAlertController(title: "ํƒ€์ดํ‹€์ž…๋‹ˆ๋‹ค", message: "๋ฉ”์„ธ์ง€์ž…๋‹ˆ๋‹ค", preferredStyle: .alert)

ํ˜น์‹œ title์ด๋‚˜ message ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ์ด์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์“ฐ์ง€ ์•Š์„ ๊ตฌ์„ฑ์š”์†Œ์˜ ๊ฐ’์„ ""์œผ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๊ฒ ์ฃ ?

// title๋งŒ ์“ฐ๊ณ  ์‹ถ์„ ๋•Œ
let alert = UIAlertController(title: "ํƒ€์ดํ‹€์ž…๋‹ˆ๋‹ค", message: "", preferredStyle: .alert)

// message๋งŒ ์“ฐ๊ณ  ์‹ถ์„ ๋•Œ
let alert = UIAlertController(title: "", message: "๋ฉ”์„ธ์ง€์ž…๋‹ˆ๋‹ค", preferredStyle: .alert)

โ‘ก Alert Action

Alert Action์—์„œ ์ง€์ •ํ•  ๋ถ€๋ถ„์€ ์—ฌ๊ธฐ์ž…๋‹ˆ๋‹ค!

        let action = UIAlertAction(title: "action", style: .default, handler: nil)

UIAlertAction์˜ ์Šคํƒ€์ผ์—๋Š” default, cancel, destructive 3๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ์š”! ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•ด๋ณผ๊ฒŒ์š”

default์— ๋น„ํ•ด cancel์€ ๋ณผ๋“œ ์Šคํƒ€์ผ๋กœ, destructive๋Š” ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ๋ฐ”๋€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชฉ์ ์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค


โ‘ข add Action

์ด์ œ ์ž˜ ๋งŒ๋“  action์„ alert์— ๋ถ™์—ฌ์ค˜์•ผ๊ฒ ์ฃ ? addAction() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ถ™์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

/* ์ฐธ๊ณ ์šฉ */
// addAction Declaration
func addAction(_ action: UIAlertAction)
alert.addAction(action)

โ‘ฃ present

์ด์ œ action์ด ๋ถ™์€ alert view๊ฐ€ ์™„์„ฑ๋์œผ๋‹ˆ๊นŒ presentํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค!

present(alert, animated: true, completion: nil)

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ด์–ด์„œ UIAlertAction์˜ handler์™€ present์˜ completion์„ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

์ „์ฒด์ฝ”๋“œ์ž…๋‹ˆ๋‹ค :)

//
//  ViewController.swift
//  AlertView
//
//  Created by ์žฅ์„œํ˜„ on 2021/02/21.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func showAlertView(_ sender: Any) {
        // 1. alert view ๋งŒ๋“ค๊ธฐ
        let alert = UIAlertController(title: "ํƒ€์ดํ‹€์ž…๋‹ˆ๋‹ค", message: "๋ฉ”์„ธ์ง€์ž…๋‹ˆ๋‹ค", preferredStyle: .alert)

        // 2. alert action ๋งŒ๋“ค๊ธฐ
        let action = UIAlertAction(title: "ํ™•์ธ", style: .default, handler: nil)

        // 3. alert์— action ๋ถ™์ด๊ธฐ
        alert.addAction(action)

        // 4. alert presentํ•˜๊ธฐ
        present(alert, animated: true, completion: nil)
    }
}

๋Œ“๊ธ€ ์˜์—ญ