iOS

[iOS] 🚨 Alert 창

녕이 2022. 1. 24. 16:37
728x90

 

Alert

 

: iOS에서 alert은 앱 또는 기기의 상태와 관련된 중요 정보를 전달하며 사용자에게 피드백을 요청하기 위해 사용된다.

  • 제목, 메세지, 하나 이상의 버튼

  • 입력을 수집하기 위해 텍스트 필드

➿ UIAlertController를 present 해서 사용

 

 


 

 

 

➿ alert창

 

  • 중요 액션 전, 경고가 필요한 경우 

  • 액션 취소 기회 제공

  • 유저 작업 한번 더 확인하거나 삭제 등의 작업 수행하거나 문제사항 알릴 때

  • 결정이 필요한 중요 정보 표시

 

 

 

➿  actionSheet창

  • 유저가 고를 수 있는 액션 목록이 여러개

  • 새 작업창 열거나 닫을 때 확인차

  • 유저 결정 되돌리거나 그 동작이 중요하지 않은 경우

 

Alert / ActionSheet

 

 

위 예제의 코드

 

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func tabAlertButton(_ sender: UIButton) {
        let alert = UIAlertController(title: "➿ALERT", message: "This is Alert", preferredStyle: .alert)
        let okayButton = UIAlertAction(title: "OK", style: .default, handler: nil)
        let cancelButton = UIAlertAction(title: "CANCEL", style: .destructive, handler: nil)
        alert.addAction(okayButton)
        alert.addAction(cancelButton)
        self.present(alert, animated: true, completion: nil)
    }
    
    @IBAction func tabActionSheetButton(_ sender: UIButton) {
        let alert = UIAlertController(title: "➿ActionSheet", message: "This is ActionSheet", preferredStyle: .actionSheet)
        let okayButton = UIAlertAction(title: "OK", style: .default, handler: nil)
        let cancelButton = UIAlertAction(title: "CANCEL", style: .cancel, handler: nil)
        alert.addAction(okayButton)
        alert.addAction(cancelButton)
        self.present(alert, animated: true, completion: nil)
    }
}

 

 


 

 

 

UIAlertController: 현재 view에서 present될 view → (title, message, actions, prefferedStyle)

  • actions: alert에 응답해 실행될 액션 (생략 가능)

  • prefferedStyle: alert 혹은 actionSheet 선택

 

 

UIAlertAction: alert/actionSheet에 대한 action 설정 → (title, isEnabled, style)

  • isEnabled: 현재 액션 가능한지

  • style: 액션 버튼 적용 스타일 (default, destructive, cancel)

    • default : 기본 형태

    • destructive: 빨간색으로 강조

    • cancel: 취소 액션

 

 

 

 

 

 

💡공부 및 기록용 블로그이므로 오류가 있을 수 있습니다.💡

만약 문제에 오류나 오타가 있다면 댓글로 알려주세요
언제나 환영합니다. 감사합니다. 화이팅!

 

728x90