본문 바로가기
🍎 iOS

Alert

by hyebin (Helia) 2022. 12. 8.

얼럿(Alert)

사용자에게 중요한 알림이나 경고 메시지를 나타내야 할 때 사용하는 개체

 

1. button 객체 추가

Library에서 Button을 찾아  스토리 보드에 추가

 

2. Aciton 함수 선언

button이 touch 되었을 때 동작할 코드를 작성하기 위한 Action함수 선언

 

3. AlertController, AlertAction 생성

AlertController를 선언해 Alert에 표시될 titile과 message 선언

ok Action과 cancel Action 생성, Action의 handler에서 해당 Action이 선택되었을 때 동작할 구문을 선언 가능

AlertController에 Action 추가 후 present 메서드 실행

 

4. 시뮬레이터 확인

Alert 버튼 클릭 시 alert이 나타남

ok 버튼을 선택하면 alert이 그냥 종료되지만, cancel 버튼을 선택하면 handler에선 선언한 대로 background의 색깔이 red로 바뀜

 

 

Alert preferredStyle

  • alert: 창을 띄워주는 느낌
  • actionSheet: 밑에서 위로 올라오는 느낌

Alert addAction style

  • default: 기본 형태
  • destructive: 빨간색으로 강조
  • cancel: 취소 액션 (actionSheet일 경우 따로 밑에 표시)

 

반응형

'🍎 iOS' 카테고리의 다른 글

MapView  (0) 2022.12.12
WebView  (0) 2022.12.08
Picker View  (0) 2022.12.08
Date Picker  (1) 2022.10.05
UIImageView  (0) 2022.09.28

댓글