*이 문서부터 애플공식문서표기대로

UIAlertController

init(title: String?, message: String?, preferredStyle: UIAlertController.Style)

매개변수

title : 경고제목

message : 경고의 이유에 대한 설명 텍스트

preferredStyle: 경고 컨트롤러를 표시할 때 사용할 스타일, 이 매개 변수를 사용하여 작업 시트 또는 모달로 구성할 수 있음

ex)


let lampOnAlert = UIAlertController(title: "경고", message: "현재 On상태", preferredStyle: UIAlertContoller.Style.alert)
  • UIAlertController 생성

func addAction(_ action: UIAlertAction)

ex)


lampOnAlert.addAction(onAction)
  • UIAlertAction을 얼럿에 추가

func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)

매개변수

viewControllerToPresent : 현재 컨트롤러의 내용 위에 표시할 컨트롤러

flag : 애니메이션을 보이려면 true값 전달

completion : 애니메이션이 끝난 후 실행할블록

ex)


present(lampOnAlert, animated: true, completion: nil)
  • present 메서드 실행

UIAlertAction

init(title: String?, style: UIAlertAction.Style, handler: ((UIAlertAction) -> Void)? = nil)

매개변수

title : 버튼의 타이틀에 사용하는 텍스트

style : 버튼에 적용할 스타일 정보, 수행되는 작업유형 전달

  • case default : 기본스타일

case cancel : 작업을 취소하고 변경하지 않은 상태로 유지하는 스타일

handler : 사용자가 작업을 선택할 때 실행 할 블록

ex)


let offAction = UIAlertAction(title: "네", style: UIAlertAction.style.default, handler : {
ACTION in self.lampImg.image = self.imgOff
self.isLampOn = false
}
  • UIAlertAction 생성, 핸들러에 클로저 생성

+)클로저

: 효율적인 코드를 작성하기 위해 함수 이름을 선언하지 않고 바로 함수의 몸체만 만들어 사용하는 일회용 함수

{

(매개변수)->(반환 타입) in

실행구문

}

  • 반환타입, 매개변수의 파라미터타입 생략 가능

{

매개변수 in

실행구문

}

***Do it! 스위프트로 아이폰 앱만들기 입문 참고

***apple developer 사이트 참고