개발언어/Swift
스위프트 UIAlertController 사용법 (알림)
우주먼지쪼가리
2022. 1. 15. 16:01
반응형
ex) 메모를 입력하지 않은 후 저장 버튼을 눌렸을 때, 사용자에게 보여줄 경고창을 만들도록 하겠습니다.
import UIKit
//이 클래스르 모두 상속 할 수 있도록 extension으로 정의해줌
extension UIViewController {
// 경고창 제목과 메세지를 받는 메소드 선언
func alert(title:String = "알림", message:String) {
// UIAlertController의 새로운 인스턴스 생성 (경고창제목, 경고메시지, 경고창 스타일)
let alert = UIAlertController(title:title, message:message, preferredStyle: .alert)
//경고창에 표시 할 버튼 생성 (버튼텍스트, 버튼스타일, 버튼클릭시 실행할 메소드)
let okAction = UIAlertAction(title : "확인", style: .default, handler: nil)
// 경고창에 버튼 추가
alert.addAction(okAction)
// 경고창을 화면에 표시
present(alert, animated: true, completion:nil)
}
}
경고창 스타일
출처 : 유투브 kxcoding 강의 중.
반응형