FE 공부/Swift

Swift - Alert Controller

꼬질꼬질두부 2022. 11. 22. 13:06
An object that displays an alert message.
사용자에게 경고하거나 다시 한번 되물을 때 주로 사용



< ActionSheet style >

UIAlertController로 모달 형태의 창을 띄움

let alert = UIAlertController(title: "알림", message: "정보를 삭제하시겠습니까?", preferredStyle: .actionSheet)
// title은 이름, message는 내용, preferredStyle은 alert 창이 뜨는 스타일

그림 1 _&nbsp;https://wnsah052.tistory.com/103

let okay = UIAlertAction(title: "확인", style: .default, handler: nil)
let delete = UIAlertAction(title: "삭제", style: .default, handler: nil)
let cancel = UIAlertAction(title: "취소", style: .default, handler: nil)
// 버튼 3개 설정 >> title은 버튼의 이름, style은 버튼의 스타일, handler는 특정 버튼이 클릭되었을 때 실행되어야 할 동작
// style은 .default & .cancel(그림1의 cancel 버튼) & .destructive(그림 1의 빨간 버튼)

alert.addAction(okay)
alert.addAction(delete)
alert.addAction(cancel
//alert에 버튼을 추가하는 과정
//추가하는 순서대로 버튼이 생김

alert.preferredAction = okay
//글자 진하게 설정하는 것

 

<Alert style _ action이 3개 이상인 경우>

 

<Alert style _ action이 2개인 경우>