[예제] UIAlertController

J.Noma·2021년 10월 27일
1

// 1. Alert 컨트롤러 인스턴스 생성 
// - title : 제목 (Optional String)
// - message : 제목 (Optional String)
// - preferredStyle : 얼럿으로 할건지 액션시트 할건지 (.alert / .actionSheet)
let alert = UIAlertController(title: "타이틀", message: "메시지", preferredStyle: .alert)

// 2. Alert 버튼 action 생성
// - title : 제목 (Optional String)
// - style : 해당 버튼의 스타일. cancel은 bold + 최좌측하단에 위치. destructive는 빨강색 (.default / .cancel / .destructive)
// - handler : 버튼이 눌리면 실행할 클로져. 안 넣어줘도 nil로 초기화됨 ((UIAlertAction) -> Void)?)
let action = UIAlertAction(title: "이전화면가자", style: .default) {
            (action) in
            self.present(이전화면)
        }
        
// 3. Alert 버튼 등록 (여러 개 가능. 좌측상단부터 add 순서대로 보여줌)
alert.addAction(action)

// 4. Alert 띄우기
// - _ : Alert 객체
// - animated : 띄울 때 부드럽게 띄울지
// - completion : present가 완료되면 실행할 클로져
present(alert, animated: true, completion: nil)
profile
노션으로 이사갑니다 https://tungsten-run-778.notion.site/Study-Archive-98e51c3793684d428070695d5722d1fe

0개의 댓글