alert 만들기
//얼럿 오브젝트 생성
let alert = UIAlertController(title: "타이틀", message: "얼럿 메세지", preferredStyle: .alert)
//얼럿에 붙을 확인/취소 버튼 오브젝트 생성
let defaultAction = UIAlertAction(title: "확인버튼이름", style: .default) { action in
/*
버튼을 눌렀을 때 수행할 코드
*/
}
}
let cancelAction = UIAlertAction(title: "취소버튼이름", style: .cancel, handler: nil)
//버튼 오브젝트를 얼럿 오브젝트에 "순서대로" 붙이기
alert.addAction(cancelAction)
alert.addAction(defaultAction)
//얼럿에 텍스트필드 붙이기
alert.addTextField { myTextField in
myTextField.placeholder = "생년월일 8자리"
myTextField.keyboardType = .numberPad
}
만든 alert 띄우기
present(alert, animated: true, completion: nil)
도큐먼트 참고
UIAlertController 클래스
사용자에게 얼럿 메세지를 표시하는 오브젝트이다.
Declaration
@MainActor class UIAlertController : UIViewController
Overview
얼럿이나 액션시트를 사용하여 표시하고싶은 메세지와 선택에 따른 액션을 설정하려면 이 클래스를 사용하세요.
설정 이후에 present(_:animated:completion:) 메소드를 사용하여 표시하세요.
UIKit이 얼럿이나 액션시트를 모달형식으로 화면에 표시합니다.
addAction(_:)메소드를 사용하여 얼럿이나 액션시트에 사용자가 반응할 각각의 액션을 추가합니다.
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
self.present(alert, animated: true, completion: nil)
UIAlertAction 클래스
얼럿에서 사용자가 버튼을 눌렀을 때 실행할 액션
Declaration
@MainActor class UIAlertAction : NSObject
Overview
만든 이후에는 UIAlertController 오브젝트에 추가하시오
'Best Practices' 카테고리의 다른 글
데이터 저장: plist (0) | 2021.10.31 |
---|---|
Sandbox의 Documents 폴더 접근하기 (0) | 2021.10.31 |