Alert View에 대해 알아보도록 하겠습니다.
기기의 상태와 관련된 중요한 정보를 전달하고 종종 피드백을 요청할 때 Alerts을 사용합니다.
Alerts는 아래와 같이 사용합니다.
title
은 제목 message
는 세부내용을 적습니다.
preferredStyle
은 .alert
을 선택합니다. .actionSheet
를 선택하면 ActionSheet로 되는 것 같습니다.
let alert = UIAlertController(title: nil, message: "메시지", preferredStyle: .alert)
그 뒤에 버튼을 추가해줍니다..
title
은 버튼에 표시될 제목이고, style
은 버튼의 스타일을 말하는데
아래 이미지를 보시면 위에부터 default, destructive, cancel 입니다.
cancel 스타일은 항상 버튼이 두개일때는 왼쪽, 3개 이상일 때는 아래에 배치됩니다.
let okAction = UIAlertAction(title: "OK", style: .default)
let noAction = UIAlertAction(title: "Cancel", style: .cancel)
그리고 버튼을 추가 해주고 present를 해줍니다.
present에서 animated를 true와 false를 선택할 수 있는데 true로 하게되면 좀 우와앙..?하고 나오는 효과가 있습니다. false로 할 때에는 아무런 효과 없이 딱 하고 나옵니다.
alert.addAction(okAction)
alert.addAction(noAction)
present(alert, animated: true)
구현 코드
let alert = UIAlertController(title: nil, message: "메시지", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
let noAction = UIAlertAction(title: "Cancel", style: .cancel)
alert.addAction(okAction)
alert.addAction(noAction)
present(alert, animated: true)